﻿using System;
using System.Xml;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Text;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmReportPublic_class.
	/// </summary>
	public class frmReportPublic_class
	{
		#region Khai bao thanh phan su dung trong class
		bool flag_dsnull;
		string sql="";
		System.Data.DataSet ds_Data;
		AccessData a=new AccessData();
		#endregion

		public frmReportPublic_class()
		{
		}		
		public void f_Tinh_chungtudauthang(ref System.Data.DataSet ds_Input,string s_Fieldno,string s_Fieldco,
			string s_Fielddk,string s_Dieukien,string s_Fieldsapxep,ref decimal d_Luykeno,ref decimal d_Luykeco)
		{
			try
			{
				d_Luykeno=0; d_Luykeco=0;
				while (ds_Input.Tables[0].Select(s_Fielddk+"='"+s_Dieukien+"'",s_Fieldsapxep).Length>0)
				{
					System.Data.DataRow dr=ds_Input.Tables[0].Select(s_Fielddk+"='"+s_Dieukien+"'",s_Fieldsapxep)[0];
					d_Luykeno+=decimal.Parse(dr[s_Fieldno].ToString());
					d_Luykeco+=decimal.Parse(dr[s_Fieldco].ToString());
					ds_Input.Tables[0].Rows.Remove(dr);
				}
				ds_Input.AcceptChanges();
			}
			catch{};
		}

		public void f_Tinh_congnodauthang(ref System.Data.DataSet ds_Input)
		{
			try
			{
				System.Data.DataRow dr_Data;
				decimal d_fsnongay=0, d_fscongay=0;
				while (ds_Input.Tables[0].Select("sapxep=99","iddt").Length>0)
				{
					System.Data.DataRow dr=ds_Input.Tables[0].Select("sapxep=99","iddt")[0];
					try { dr_Data=ds_Input.Tables[0].Select("iddt='"+dr["iddt"].ToString().Trim()+"' and sapxep<>99","iddt")[0];}
					catch { dr_Data=null; }
					if (dr_Data!=null)
					{
						d_fsnongay=decimal.Parse(dr["sotienno"].ToString());
						d_fscongay=decimal.Parse(dr["sotienco"].ToString());
						dr_Data["tondauno"]=decimal.Parse(dr_Data["tondauno"].ToString())+d_fsnongay;
						dr_Data["tondauco"]=decimal.Parse(dr_Data["tondauco"].ToString())+d_fscongay;
					}
					else
					{
						dr_Data=ds_Input.Tables[0].NewRow();
						dr_Data["thangg"]=dr["thangg"];
						dr_Data["iddt"]=dr["iddt"];
						dr_Data["madt"]=dr["madt"];
						dr_Data["tendt"]=dr["tendt"];
						dr_Data["sotienno"]=dr["sotienno"];
						dr_Data["sotienco"]=dr["sotienco"];
						dr_Data["tondauno"]=dr["tondauno"];
						dr_Data["tondauco"]=dr["tondauco"];
						dr_Data["cuoikyno"]=dr["cuoikyno"];
						dr_Data["cuoikyco"]=dr["cuoikyco"];
						dr_Data["ghichu"]=dr["ghichu"];
						dr_Data["loaictu"]=1;
						dr_Data["sapxep"]=2;
						ds_Input.Tables[0].Rows.Add(dr_Data);
					}

					ds_Input.Tables[0].Rows.Remove(dr);
				}
				ds_Input.AcceptChanges();
			}
			catch{};
		}

		//Tinh theo tam tam ung dau thang.
		public void f_Tinh_tamungdauthang(ref System.Data.DataSet ds_Input)
		{
			try
			{
				System.Data.DataRow dr_Data;
				decimal d_fsnongay=0, d_fscongay=0;
				while (ds_Input.Tables[0].Select("sapxep=99","iddt").Length>0)
				{
					System.Data.DataRow dr=ds_Input.Tables[0].Select("sapxep=99","iddt")[0];
					try { dr_Data=ds_Input.Tables[0].Select("iddt='"+dr["iddt"].ToString().Trim()+"' and sapxep<>99","iddt")[0];}
					catch { dr_Data=null; }
					if (dr_Data!=null)
					{
						d_fsnongay=decimal.Parse(dr["sotienno"].ToString());
						d_fscongay=decimal.Parse(dr["sotienco"].ToString());
						dr_Data["sodu"]=decimal.Parse(dr_Data["sodu"].ToString())+(d_fsnongay-d_fscongay);
						dr_Data["fsnonam"]=decimal.Parse(dr_Data["fsnonam"].ToString())+d_fsnongay;
						dr_Data["fsconam"]=decimal.Parse(dr_Data["fsconam"].ToString())+d_fscongay;
					}
					else
					{
						dr_Data=ds_Input.Tables[0].NewRow();
						dr_Data["MAKP"]=dr["MAKP"];
						dr_Data["TENKP"]=dr["TENKP"];
						dr_Data["MADT"]=dr["MADT"];
						dr_Data["TENDT"]=dr["TENDT"];
						dr_Data["IDDT"]=dr["IDDT"];
						dr_Data["DIACHIDT"]=dr["DIACHIDT"];
						dr_Data["DIENTHOAIDT"]=dr["DIENTHOAIDT"];
						dr_Data["NGAYGS"]=dr["NGAYGS"];
						dr_Data["NGAYG"]=dr["NGAYG"];
						dr_Data["DIENGIAI"]=dr["DIENGIAI"];
						dr_Data["SOTIENNO"]=dr["SOTIENNO"];
						dr_Data["SOTIENCO"]=dr["SOTIENCO"];
						dr_Data["SODU"]=dr["SODU"];
						dr_Data["FSNONAM"]=dr["FSNONAM"];
						dr_Data["FSCONAM"]=dr["FSCONAM"];
						dr_Data["loaictu"]=0;
						dr_Data["sapxep"]=1;
						ds_Input.Tables[0].Rows.Add(dr_Data);
					}

					ds_Input.Tables[0].Rows.Remove(dr);
				}
				ds_Input.AcceptChanges();
			}
			catch{};
		}


		public System.Data.DataSet f_Tonghopchungtu(LibKetoan.AccessData a,int i_Loaibc,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_yy,bool b_Checkngay,string s_IDdoituong,string s_muc, string s_IDkm,string s_IDkm2,string s_IDkm3,string s_IDkm4,int i_dkgroup)
		{
			string s_CTdk="";
			flag_dsnull=false;

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";

            switch (i_Loaibc)
			{
				case 0: 
					s_CTdk="(ctct.tknoa in ("+s_IDtk+"))";
					s_CTdk+=" or (ctct.tkcoa in ("+s_IDtk+"))";
					break;
				case 1: 
					s_CTdk="ctct.tknoa in ("+s_IDtk+")";
					break;
				case 2: 
					s_CTdk="ctct.tkcoa in ("+s_IDtk+")";
					break;
				default: 
					s_CTdk="(ctct.tknoa in ("+s_IDtk+"))";
					s_CTdk+=" or (ctct.tkcoa in ("+s_IDtk+"))";
					break;
			}			
			sql=  "select ct.soctg as soctg,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,ct.noidung as diengiai,";
			sql+=" hd.ten as hoatdong,ch.ten as chuong,dt.tendt as doituong,bc.ten as nhombc,ctct.tknoa as tkno,";
			sql+=" ctct.tkcoa as tkco,case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
//			sql+="  (case when ctct.tknoa in("+s_IDtk+") then dmtkco.stk else (case when";
//			sql+=" ctct.tkcoa in("+s_IDtk+") then dmtkno.stk else '' end) end) tkdoiung,";
			//sql+=" kmco.ten as kmco, kmno.ten as kmno, kmno2.ten as kmno2, kmco2.ten as kmco2, kmco3.ten as kmco3, kmno3.ten as kmno3,kmco4.ten as kmco4,kmno4.ten as kmno4,";			
			sql+=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
			sql+=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2, ";
			sql+=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
			sql+=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
			sql+=" (case when ("+s_CTdk+") then ctct.stien else 0 end) sotien";
			sql+=" "+s_DKgroup+" ";
			/*sql+=" from a_ct"+s_yy+" ct,a_ctct"+s_yy+" ctct,a_dmlk lk,a_dmdt dt,a_dmch chno,a_dmch chco,";
			sql+=" a_hoatdong hd,a_dmchuong ch,a_dmnhombc bc,";
			sql+=" a_dmkhoanmuc kmno,a_dmkhoanmuc kmco, a_dmkhoanmuc kmno2,a_dmkhoanmuc kmco2,";
			sql+=" a_dmkhoanmuc kmno3,a_dmkhoanmuc kmco3,a_dmkhoanmuc kmno4,a_dmkhoanmuc kmco4";
			*/
			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
			sql+=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
			sql+=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
			sql+=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";
			sql+=" left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
			
			sql+=" where ("+s_CTdk+")";
			if (s_IDchuong!="")
				sql+=" and ct.chuong='"+s_IDchuong+"'";
			if (s_IDlk!="")
				sql+=" and lk.id='"+s_IDlk+"'";
			if (s_IDhoatdong!="")
				sql+=" and ct.hoatdong='"+s_IDhoatdong+"'";
			if (s_IDnhombc!="")
				sql+=" and ct.idbc='"+s_IDnhombc+"'";
			if(s_IDdoituong!="")sql+=" and dt.id="+s_IDdoituong;
			if(s_muc!="")sql+=" and (chno.id in ("+s_muc+") or chco.id in("+s_muc+"))";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";
			
			if (b_Checkngay)
				sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Tungay+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else 
				sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
				//sql+=" and to_date(to_char(ct.ngayg,'mm/yyyy'),'mm/yyyy') between to_date('"+s_Tungay+"','mm/yyyy') and to_date('"+s_Denngay+"','mm/yyyy')";
			sql+=" order by ct.soctg";

			try
			{
				ds_Data=a.get_data(sql).Copy();
				if ((ds_Data==null || ds_Data.Tables[0].Rows.Count<=0)) flag_dsnull=true;
				//ds_Data.WriteXml("..\\xml\\KT_Bangtonghopchungtugoc.xml",XmlWriteMode.WriteSchema);
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Data;
		}

		public System.Data.DataSet f_Tonghopchungtugoccungloai(LibKetoan.AccessData a,int i_Loaibc,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_yy,bool b_Checkngay,string s_IDdoituong,string s_muc, string s_IDkm,string s_IDkm2,string s_IDkm3,string s_IDkm4,int i_dkgroup)
		{
			string s_CTdk="";
			flag_dsnull=false;

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";

			switch (i_Loaibc)
			{
				case 19: 
					s_CTdk="ct.tknoa in ("+s_IDtk+")";
					break;
				case 21: 
					s_CTdk="ct.tkcoa in ("+s_IDtk+")";
					break;
				default: 
					s_CTdk="(ct.tknoa in ("+s_IDtk+"))";
					s_CTdk+=" or (ct.tkcoa in ("+s_IDtk+"))";
					break;
			}			
			sql=  "select distinct ct.soctg as soctg,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,ct.noidung as diengiai,";
//			sql+=" gs.noidung as noidunggs,";
			sql+=" hd.ten as hoatdong,ch.ten as chuong,dt.tendt as doituong,bc.ten as nhombc,ct.tknoa as tkno,";
			sql+=" ct.tkcoa as tkco,case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,'' as stkno,'' as stkco,";
			sql+=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
			sql+=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2, ";
			sql+=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
			sql+=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
			sql+=" case when ct.tknoa in ("+s_IDtk+") then ct.stien else 0 end as sotienthu, ";
			sql+=" case when ct.tkcoa in ("+s_IDtk+") then ct.stien else 0 end as sotienchi, ";
			sql+=" (case when ("+s_CTdk+") then ct.stien else 0 end) sotien,'' tentkno,'' tentkco ";
			sql+=" "+s_DKgroup+" ";
			/*sql+=" from a_ct"+s_yy+" ct,a_ctct"+s_yy+" ctct,a_dmlk lk,a_dmdt dt,a_dmch chno,a_dmch chco,";
			sql+=" a_hoatdong hd,a_dmchuong ch,a_dmnhombc bc,";
			sql+=" a_dmkhoanmuc kmno,a_dmkhoanmuc kmco, a_dmkhoanmuc kmno2,a_dmkhoanmuc kmco2,";
			sql+=" a_dmkhoanmuc kmno3,a_dmkhoanmuc kmco3,a_dmkhoanmuc kmno4,a_dmkhoanmuc kmco4";
			*/
			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			//sql+=" inner join "+a.user+".a_ctgs"+s_yy+" gs on ctct.ctgs=gs.id ";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
			sql+=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
			sql+=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
			sql+=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";
			sql+=" left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
			
			sql+=" where ("+s_CTdk+") and ctct.tonghop=1 ";
			if (s_IDchuong!="")
				sql+=" and ct.chuong='"+s_IDchuong+"'";
			if (s_IDlk!="")
				sql+=" and lk.id='"+s_IDlk+"'";
			if (s_IDhoatdong!="")
				sql+=" and ct.hoatdong='"+s_IDhoatdong+"'";
			if (s_IDnhombc!="")
				sql+=" and ct.idbc='"+s_IDnhombc+"'";
			if(s_IDdoituong!="")sql+=" and dt.id="+s_IDdoituong;
			if(s_muc!="")sql+=" and (chno.id in ("+s_muc+") or chco.id in("+s_muc+"))";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";
			
			if (b_Checkngay)
				sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Tungay+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else 
				sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
			//sql+=" and to_date(to_char(ct.ngayg,'mm/yyyy'),'mm/yyyy') between to_date('"+s_Tungay+"','mm/yyyy') and to_date('"+s_Denngay+"','mm/yyyy')";
			sql+=" order by ct.soctg";

			try
			{
				ds_Data=a.get_data(sql).Copy();
				for(int i=0;i<ds_Data.Tables[0].Rows.Count;i++)
				{
					System.Data.DataRow dr=ds_Data.Tables[0].Select("soctg<>''","soctg")[i];
					dr["stkno"]=a.get_stk_id(int.Parse(dr["tkno"].ToString()));
					dr["tentkno"]=a.ttk_id(int.Parse(dr["tkno"].ToString()));
					dr["stkco"]=a.get_stk_id(int.Parse(dr["tkco"].ToString()));
					dr["tentkco"]=a.ttk_id(int.Parse(dr["tkco"].ToString()));
				}
				ds_Data.AcceptChanges();
				if ((ds_Data==null || ds_Data.Tables[0].Rows.Count<=0)) flag_dsnull=true;
				ds_Data.WriteXml("..\\xml\\KT_Bangtonghopchungtugoc.xml",XmlWriteMode.WriteSchema);
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Data;
		}
		public System.Data.DataSet f_Sodangkyctughiso(LibKetoan.AccessData a,string s_IDchuong,string s_IDchucnang,string s_IDloaictu,string s_IDlk,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_yy,bool b_Checkngay)
		{
			sql=  "select ctgs,to_char(ctgs.ngaygs,'dd/mm/yyyy') as ngaygs,stien,noidung";
			sql+=" from "+a.user+".a_ctgs"+s_yy +" ctgs";
			if (b_Checkngay)
				sql+=" where to_date(to_char(ngaygs,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Tungay+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else 
				sql+=" where thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";

			if (s_IDchuong!="")
				sql+=" and chuong='"+s_IDchuong+"'";
			if (s_IDchucnang!="")
				sql+=" and chucnang='"+s_IDchucnang+"'";
			if (s_IDloaictu!="")
				sql+=" and lctug='"+s_IDloaictu+"'";
			if (s_IDlk!="")
				sql+=" and lk='"+s_IDlk+"'";
			if (s_IDnhombc!="")
				sql+=" and idbc='"+s_IDnhombc+"'";
			sql+=" order by to_char(ctgs.ngaygs,'yyyymmdd'),ctgs";
			
			string sql_count="select count(*) as count";
			sql_count+=" from "+a.user+".a_ctgs"+s_yy;
			sql_count+=" where thanggs between '"+s_Tungay+"' and '"+s_Denngay+"' ";

			try
			{
				ds_Data=a.get_data(sql).Copy();
				if ((ds_Data==null || ds_Data.Tables[0].Rows.Count<=0)) flag_dsnull=true;
				//ds_Data.WriteXml("..\\xml\\KT_Bangtonghopchungtu.xml",XmlWriteMode.WriteSchema);
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Data;
		}
		public System.Data.DataSet f_SocaiTonghop(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDkm,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_mm2,string s_yy,string s_yyyy,bool b_Checkngay,string s_IDkm2,string s_IDkm3,string s_IDkm4,string s_IDdoituong,string s_muc,int i_dkgroup)
		{
			
			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";

			
			sql=  "select ct.thanggs as thangg,to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,ct.soctg,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,";
			sql+=" hd.ten as hoatdong,ch.ten as chuong,dt.tendt as doituong,bc.ten as nhombc,";
			sql+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
			//sql+=" kmco.ten as kmco, kmno.ten as kmno, kmno2.ten as kmno2, kmco2.ten as kmco2, kmco3.ten as kmco3, kmno3.ten as kmno3,kmco4.ten as kmco4,kmno4.ten as kmno4,";
			sql+=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
			sql+=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2, ";
			sql+=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
			sql+=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
			sql+=" nvl(ctct.noidung,ct.noidung)as diengiai,(case when ctct.tknoa in("+s_IDtk+")then dmtkco.stk else (case when";
			sql+=" ctct.tkcoa in("+s_IDtk+")then dmtkno.stk else '' end) end) as tkdoiung,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+")then ctct.stien else 0 end)as sotienno,";
			sql+=" (case when ctct.tkcoa in("+s_IDtk+")then ctct.stien else 0 end)as sotienco,";
			sql+=" 0 as tondauno,0 as tondauco,0 as fsnoquy,0 as fscoquy,0 as fsnonam,0 as fsconam,'' as ghichu,'' as tinhchat,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then 1 else 2 end) as loaictu,to_number(to_char(ct.ngayg,'yyyymm')||'2') as sapxep";
			sql+=" "+s_DKgroup+" ";

			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmtk dmtkno on ctct.tknoa=dmtkno.id";
			sql+=" left join "+a.user+".a_dmtk dmtkco on ctct.tkcoa=dmtkco.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
			sql+=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
			sql+=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
			sql+=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";
			sql+=" left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
			
			sql+=" where ctct.tonghop=1";			
			if (b_Checkngay)
				sql+=" and to_date(to_char(ngaygs,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Tungay+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else 
				sql+=" and thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
			//sql+=" and to_date(to_char(ct.ngayg,'mm/yyyy'),'mm/yyyy') between to_date('"+s_Tungay+"','mm/yyyy') and to_date('"+s_Denngay+"','mm/yyyy')";
			if (s_IDlk!="") sql+=" and lk.id='"+s_IDlk+"'";
			if (s_IDchuong!="")
				sql+=" and ct.chuong='"+s_IDchuong+"'";
			if (s_IDhoatdong!="")
				sql+=" and ct.hoatdong='"+s_IDhoatdong+"'";
			if (s_IDnhombc!="")
				sql+=" and ct.idbc='"+s_IDnhombc+"'";
			if(s_IDdoituong!="")sql+=" and dt.id="+s_IDdoituong;
			if(s_muc!="")sql+=" and (chno.id in ("+s_muc+") or chco.id in ("+s_muc+"))";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";	
			
			sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
			sql+=" order by to_char(ct.ngayg,'yyyymmdd'),ct.soctg";

			try
			{
				ds_Data=a.get_data(sql).Copy();
			}
			catch{};

			try
			{
				decimal d_Daukyno=0,d_Daukyco=0,d_FSquyno=0,d_FSquyco=0,d_FSnamno=0,d_FSnamco=0;
				//Them dong load so du dau ky.
				int mm1=int.Parse(s_mm);
				int mm2=int.Parse(s_mm2);
				for (int j=mm1;j<=mm2;j++)
				{
					try
					{ 
						System.Data.DataRow drSodu = ds_Data.Tables[0].NewRow();
						decimal sumAll=0;
						string s_mmyyyy;
						drSodu["thangg"]=j.ToString().PadLeft(2,'0')+"/"+s_yy.ToString().PadLeft(4,'0');
						drSodu["ngaygs"]=DateTime.Parse(j.ToString().PadLeft(2,'0')+"/01/"+s_yy.ToString());
						drSodu["soctg"]="";
						drSodu["ngayg"]=DateTime.Parse(j.ToString().PadLeft(2,'0')+"/01/"+s_yy.ToString());
						drSodu["diengiai"]="Số dư đầu tháng";
						drSodu["tkdoiung"]="";

						//Tham so cuoi: 1-lay so du no hoac phat sinh no, 2-lay so du co hoac phat sinh co.
						drSodu["sotienno"]=0;
						drSodu["sotienco"]=0;
						
						drSodu["ghichu"]="";
						drSodu["loaictu"]=0;
						drSodu["sapxep"]=int.Parse(s_yy.ToString()+j.ToString().PadLeft(2,'0')+"1");

						//Set lai phat sinh quy khi qua quy moi.
						if (((j-1) % 3)==0)
						{
							d_FSquyno=0;
							d_FSquyco=0;
						}
						//

						if (j==mm1)
						{						
							d_Daukyno=a.get_Sodudauky(j.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);
							d_Daukyco=a.get_Sodudauky(j.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);
							
							d_FSquyno=a.get_FSluykequy(j.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);
							d_FSquyco=a.get_FSluykequy(j.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);

							d_FSnamno=a.get_FSluykenam(j.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);
							d_FSnamco=a.get_FSluykenam(j.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);
							sumAll+=d_Daukyno+d_Daukyco+d_FSquyno+d_FSquyco+d_FSnamno+d_FSnamco;
						}
						else
						{
							s_mmyyyy=(j-1).ToString().PadLeft(2,'0')+"/"+s_yyyy;
							decimal d_Luyke_ds_no=a.get_FSluyke_ds("thangg='"+s_mmyyyy+"'",ds_Data,"sotienno","sotienco",1);
							decimal d_Luyke_ds_co=a.get_FSluyke_ds("thangg='"+s_mmyyyy+"'",ds_Data,"sotienno","sotienco",2);

							d_Daukyno=(d_Daukyno+d_Luyke_ds_no) - Math.Min((d_Daukyno+d_Luyke_ds_no),(d_Daukyco+d_Luyke_ds_co));
							d_Daukyco=(d_Daukyco+d_Luyke_ds_co) - Math.Min((d_Daukyno+d_Luyke_ds_no),(d_Daukyco+d_Luyke_ds_co));

							d_FSquyno+=d_Luyke_ds_no;
							d_FSquyco+=d_Luyke_ds_co;

							d_FSnamno+=d_Luyke_ds_no;
							d_FSnamco+=d_Luyke_ds_co;
							sumAll+=d_Daukyno+d_Daukyco+d_FSquyno+d_FSquyco+d_FSnamno+d_FSnamco;
						}						
						drSodu["tondauno"]=d_Daukyno;
						drSodu["tondauco"]=d_Daukyco;
						
						drSodu["fsnoquy"]=d_FSquyno;
						drSodu["fscoquy"]=d_FSquyco;

						drSodu["fsnonam"]=d_FSnamno;
						drSodu["fsconam"]=d_FSnamco;

						ds_Data.Tables[0].Rows.Add(drSodu);
						ds_Data.AcceptChanges();
					}
					catch{}	;
				}
			}
			catch{};
			try
			{
				if (ds_Data==null || ds_Data.Tables[0].Rows.Count<=0 )flag_dsnull=true;
				else
				{
					//ds_Data.WriteXml("..\\xml\\kt_Socai.xml",XmlWriteMode.WriteSchema);
//					s_Report="KT_Socai.rpt";
//					string title=""; 
//					if (mm1==mm2) title="Tháng "+mm1+" năm "+yy;
//					else title="Từ tháng "+mm1+" đến tháng "+mm2+" năm "+yy;
//
//					string title2="Tài khoản : "+txtSotaikhoan.Text+" - "+txtTentaikhoan.Text;
//					string title3="";
//					
//					if (loai==1)
//					{
//						frmReport f=new frmReport(a,dsData,title,title2,title3,s_Report.ToString());
//						f.ShowDialog();
//					}
//					else Print.Printer(a,dsData,title,title2,title3,s_Report.ToString(),1,1);
				}
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Data;
		}

		public System.Data.DataSet f_Sochungtughiso(LibKetoan.AccessData a,string s_IDchuong,string s_IDchucnang,string s_IDloaictu,string s_IDlk, string s_IDtk, string s_IDnhombc,string s_Tungay,string s_Denngay,string s_yy,bool b_Checkngay)
		{
			sql=  "select ctgs.ctgs,to_char(ctgs.ngaygs,'dd/mm/yyyy') as ngaygs,ctgs.stien as stiengs,ctgs.noidung as ghichu,";
			sql+=" to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygsct,ct.soctg as soctg,nvl(ctct.noidung,ct.noidung) as trichyeu,";
			sql+=" tkno.stk as stkno,tkco.stk as stkco,ctct.stien as sotien";
			sql+=" from "+a.user+".a_ctgs"+s_yy+" ctgs left join "+a.user+".a_ctct"+s_yy+" ctct on ctgs.id=ctct.ctgs";
			sql+=" left join "+a.user+".a_dmtk tkno on ctct.tknoa=tkno.id";
			sql+=" left join "+a.user+".a_dmtk tkco on ctct.tkcoa=tkco.id";
			sql+=" inner join "+a.user+".a_ct"+s_yy+" ct on ctct.idct=ct.id";
			if (b_Checkngay)
				sql+=" where to_date(to_char(ctgs.ngaygs,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Tungay+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else 
				sql+=" where ctgs.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
			if (s_IDchuong!="")
				sql+=" and ctgs.chuong='"+s_IDchuong+"' and ct.chuong='"+s_IDchuong+"'";
			if (s_IDchucnang!="")
				sql+=" and ctgs.chucnang='"+s_IDchucnang+"' and ct.chucnang='"+s_IDchucnang+"'";
			if (s_IDloaictu!="")
				sql+=" and ctgs.lctug='"+s_IDloaictu+"'";
			if (s_IDlk!="")
				sql+=" and ctgs.lk='"+s_IDlk+"'";
            if(s_IDtk !="")
				sql+=" and tkco.stk='"+s_IDtk+"'";
			if (s_IDnhombc!="")
				sql+=" and ctgs.idbc='"+s_IDnhombc+"'";
			sql+=" order by to_char(ctgs.ngaygs,'yyyyddmm'),ctgs.ctgs,to_char(ct.ngaygs,'yyyyddmm'),ct.soctg,nvl(ctct.noidung,ct.noidung)";
			
			string sql_count="select count(*) as count";
			sql_count+=" from "+a.user+".a_ctgs"+s_yy;
			sql_count+=" where thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";

			try
			{
				ds_Data=a.get_data(sql).Copy();
				if ((ds_Data==null || ds_Data.Tables[0].Rows.Count<=0)) flag_dsnull=true;
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Data;
		}

		public System.Data.DataSet f_Soquytienmat(LibKetoan.AccessData a,string s_IDchuong,string s_IDdoituong,string s_muc,string s_IDtk,string s_IDlk,string s_IDkm, string s_IDkm2,string s_IDkm3, string s_IDkm4,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_yy,string s_yyyy,bool b_Checkngay,int i_dkgroup)
		{
			decimal d_fsnongay=0, d_fscongay=0;			

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";
			
			string s_DKSelectgroup="",s_DKGroupbygroup="",s_DKfrom="";
			switch(i_dkgroup)
			{
				case 0:
					s_DKSelectgroup=" ch.ten as chuong,ch.id as idchuong,";
					s_DKSelectgroup+=" '' as iddoituong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as doituong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" ch.ten,ch.id,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";					
					break;
				case 1:
					s_DKSelectgroup=" dt.tendt as doituong,dt.id as iddoituong,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" dt.tendt,dt.id,";									
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					break;
				case 2:
					s_DKSelectgroup=" hd.ten as hoatdong,hd.id as idhoatdong,";
					s_DKSelectgroup+=" '' as idchuong,'' as iddoituong, '' as idnhombc,'' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong,'' as doituong, '' as nhombc,'' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" hd.ten,hd.id,";
					s_DKfrom=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					break;
				case 3:
					s_DKSelectgroup=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
					s_DKSelectgroup+=" case when(chno.ttk <>' ') then chno.id else chco.id end as idmuc,";
					s_DKSelectgroup+=" '' as idchuong,'' as iddoituong, '' as idnhombc,'' as idhoatdong, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong,'' as doituong, '' as nhombc,'' as hoatdong, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" case when(chno.ttk <>' ') then chno.id else chco.id end,";
					s_DKGroupbygroup+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
					s_DKfrom+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
					break;
				case 4:
					s_DKSelectgroup=" bc.ten as nhombc,bc.id as idnhombc,";
					s_DKSelectgroup+=" '' as idchuong, '' as iddoituong,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as doituong,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" bc.ten,bc.id,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom="left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
					break;
				case 5:
					s_DKSelectgroup=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
					s_DKSelectgroup+=" case when (kmno.ten <>' ') then kmno.id else kmco.id end as idkm,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as iddoituong, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as doituong, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" case when(kmno.ten <>' ') then kmno.ten else kmco.ten end,";
					s_DKGroupbygroup+=" case when(kmno.ten <>' ') then kmno.id else kmco.id end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
					break;
				case 6:
					s_DKSelectgroup=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2,";
					s_DKSelectgroup+=" case when (kmno2.ten <>' ') then kmno2.id else kmco2.id end as idkm2,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as iddoituong, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as doituong, '' as km3, '' as km4,";
					s_DKGroupbygroup=" case when(kmno2.ten<>' ') then kmno2.ten else kmco2.ten end,";
					s_DKGroupbygroup+=" case when(kmno2.ten<>' ') then kmno2.id else kmco2.id end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
					break;
				case 7:
					s_DKSelectgroup=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
					s_DKSelectgroup+=" case when (kmno3.ten<>' ') then kmno3.id else kmco3.id end as idkm3,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as iddoituong, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as doituong, '' as km4,";
					s_DKGroupbygroup=" case when(kmno3.ten<>' ' ) then kmno3.ten else kmco3.ten end,";
					s_DKGroupbygroup+=" case when(kmno3.ten<>' ' ) then kmno3.id else kmco3.id end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
					break;
				case 8:
					s_DKSelectgroup=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
					s_DKSelectgroup+=" case when (kmno4.ten<>' ') then kmno4.id else kmco4.id end as idkm4,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as iddoituong,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as doituong,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";	
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";s_DKGroupbygroup=" case when(kmno4.ten<>' ') then kmno4.ten else kmco4.ten end,";
					s_DKGroupbygroup+=" case when(kmno4.ten<>' ') then kmno4.id else kmco4.id end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					break;
				default: 					
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,'' as iddoituong,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,'' as doituong,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					break;
			}
			
			flag_dsnull=false;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;
			sql=  "select to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,0 as sodu_no,0 as sodu_co,0 as fsluyke_no, 0 as fsluyke_co,";
			sql+=" "+s_DKSelectgroup+"";
			sql+=" (case when (nghang.maso='PT' and ct.tknoa in("+s_IDtk+")) then ct.soctg";
			sql+=" else '' end) as soctgthu,(case when (nghang.maso='PC' and ct.tkcoa in("+s_IDtk+")) then ct.soctg else '' end)";
			sql+=" as soctgchi,nvl(dt.tendt,ctk.tendt) as hoten,ct.noidung as diengiai,sum(case when (nghang.maso='PT' and";
			sql+=" ctct.tknoa in("+s_IDtk+")) then ctct.stien else 0 end)as sotienthu,sum(case when (nghang.maso='PC' and ctct.tkcoa in("+s_IDtk+")";
			sql+=" )then ctct.stien else 0 end)as sotienchi,0 as stton,0 as fsnothtruoc,0 as fscothtruoc,'' as ghichu,";
			sql+=" (case when (nghang.maso='PT' and ct.tknoa in("+s_IDtk+")) then 1 else 2 end) as loaictu,";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end)as sapxep";
			else sql+=" 2 as sapxep";
			sql+=" "+s_DKgroup+" ";
	
			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_bcnghang nghang on ct.lctug=nghang.id"; 		
			
			sql+=" left join "+a.user+".a_ctk"+s_yy+" ctk on ct.id=ctk.id";
			sql+=" left join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";	
			sql+=" "+s_DKfrom+"";

			sql+=" where nghang.maso in('PT','PC')";
			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";			

			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
				//sql+=" and to_date(to_char(ct.ngayg,'mm/yyyy'),'mm/yyyy') between to_date('"+s_Tungay+"','mm/yyyy') and to_date('"+s_Denngay+"','mm/yyyy')";
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and lk.id="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;
			if(s_IDdoituong!="")sql+=" and dt.id="+s_IDdoituong;
			if(s_muc!="")sql+=" and (ctct.mtmno in("+s_muc+") or ctct.mtmco in("+s_muc+"))";
			sql+=" group by 0,ct.ngaygs,ct.ngayg,";
			sql+=" "+s_DKGroupbygroup+"";
			sql+=" (case when (nghang.maso='PT' and ct.tknoa in("+s_IDtk+")) then ct.soctg";
			sql+=" else '' end),(case when (nghang.maso='PC' and ct.tkcoa in("+s_IDtk+")) then ct.soctg else '' end),";
			sql+=" nvl(dt.tendt,ctk.tendt),ct.noidung,0,0,0,'',(case when (nghang.maso='PT' and ct.tknoa in("+s_IDtk+")) then 1 else 2 end),";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end)";
			else sql+=" 2";
			sql+=" order by to_char(ct.ngayg,'yyyymmdd'),loaictu,soctgchi,soctgthu";

			try
			{
				ds_Data=a.get_data(sql).Copy();
//				if (ds_Data==null || ds_Data.Tables[0].Rows.Count<=0) flag_dsnull=true;
//				else
//				{
					if(i_dkgroup!=-1)
					{
						DataSet ds_Data2=new DataSet();
						ds_Data2=a.get_dsSodudauky(s_mm,s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4,i_dkgroup);
						if(ds_Data2!=null)
							ds_Data.Merge(ds_Data2);
						ds_Data.AcceptChanges();
						for (int i=1;i<int.Parse(s_mm);i++)
						{
							DataSet ds_Data3=new DataSet();
							ds_Data3=a.get_FSluykenamdt(s_mm,s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,i_dkgroup,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4,i);
						
							if(ds_Data3!=null)
								ds_Data.Merge(ds_Data3);
						}
						ds_Data.AcceptChanges();
					}
//				}
			}
			catch
			{
				flag_dsnull=true;
			}			
			//Tinh luy ke tu dau thang den ngay hien tai.
			//ds_Data.WriteXml("..\\xml\\KT_Soquytienmat.xml",XmlWriteMode.WriteSchema);
			if (b_Checkngay) f_Tinh_chungtudauthang(ref ds_Data,"sotienthu","sotienchi","sapxep","99","ngayg",ref d_fsnongay,ref d_fscongay);

			try
			{
				decimal d_Dauky=0,d_FSnamno=0,d_FSnamco=0,sumAll=0;
				//Them dong load so du dau ky.
			
				System.Data.DataRow dr_Sodu = ds_Data.Tables[0].NewRow();
				dr_Sodu["ngaygs"]="01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy;//DateTime.ParseExact("01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy,"dd/MM/yyyy",null);
				dr_Sodu["ngayg"]="01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy;//DateTime.ParseExact("01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy,"dd/MM/yyyy",null);
				dr_Sodu["soctgthu"]="";
				dr_Sodu["soctgchi"]="";
				dr_Sodu["hoten"]="";
				dr_Sodu["diengiai"]="Số dư đầu kỳ";
				dr_Sodu["sotienthu"]=0;
				dr_Sodu["sotienchi"]=0;

				d_Dauky=a.get_Sodudauky(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,3,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+(d_fsnongay-d_fscongay);
				d_FSnamno=a.get_FSluykenam(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+d_fsnongay;
				d_FSnamco=a.get_FSluykenam(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+d_fscongay;
				sumAll=d_Dauky+d_FSnamno+d_FSnamco;

				dr_Sodu["stton"]=d_Dauky;
				dr_Sodu["fsnothtruoc"]=d_FSnamno;
				dr_Sodu["fscothtruoc"]=d_FSnamco;
				dr_Sodu["ghichu"]="";
				dr_Sodu["loaictu"]=0;
				dr_Sodu["sapxep"]=1;

				ds_Data.Tables[0].Rows.Add(dr_Sodu);
				ds_Data.AcceptChanges();
				if (flag_dsnull && sumAll==0) flag_dsnull=true;
				else flag_dsnull=false;
			}
			catch{};
			if (flag_dsnull) return null;
			else return ds_Data;
			//End tinh so du dau ky
		} 

		//Tinh so du no, so du co cho cac doi tuong.
		private System.Data.DataSet f_Laysodu_tamung(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDhoatdong,string s_IDkhoaphong,string s_IDdoituong,string s_IDnhombc,string s_mm,string s_yy,string s_yyyy)
		{
			System.Data.DataSet ds_Sodu;
			string s_ddmmyyyy="01/"+s_mm+"/"+s_yyyy;
			string s_Fsno="0", s_Fsco="0";
			if (int.Parse(s_mm)>1)
			{
				s_Fsno="fsno01"; s_Fsco="fsco01";
				for (int i=2; i< int.Parse(s_mm); i++)
				{
					s_Fsno+="+fsno"+i.ToString().PadLeft(2,'0');
					s_Fsco+="+fsco"+i.ToString().PadLeft(2,'0');
				}
			}
			//Tinh so du dau ky cho tat ca doi tuong theo tung khoa.
			sql=  "select distinct nvl(kp.makp,'---') as makp,nvl(kp.tenkp,'---') as tenkp,nvl(upper(dt.mavt),'KHAC') as madt,";
			sql+=" nvl(dt.tendt,'DT Khac') as tendt,sd.madt as iddt,dt.diachi  as diachidt,dt.dienthoai as dienthoaidt,";
			sql+=" dt.email as emaildt,dt.fax as faxdt,to_date('"+s_ddmmyyyy+"','dd/mm/yyyy') as ngaygs,";
			sql+=" to_date('"+s_ddmmyyyy+"','dd/mm/yyyy') as ngayg,'' as soctg,'So du dau ky' as diengiai,";
			sql+=" 0 as sotienno,0 as sotienco,";
			sql+=" sum(sd.sdno"+s_mm+" - sd.sdco"+s_mm+") as sodu,sum("+s_Fsno+") as fsnonam,sum("+s_Fsco+") as fsconam,'' as ghichu,";
			sql+=" 0 as loaictu,1 as sapxep";
			sql+=" from "+a.user+".a_sd"+s_yy+" sd left join "+a.user+".a_dmdt dt on sd.madt=dt.id";
			sql+=" left join "+a.user+".a_btdkp kp on dt.makp=kp.id";
			sql+=" where sd.stka in ("+s_IDtk+")";
			if (s_IDchuong!="") sql+=" and sd.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and sd.lk="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and sd.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and sd.idbc="+s_IDnhombc;
			if (s_IDkhoaphong!="") sql+=" and dt.makp='"+s_IDkhoaphong+"'";
			if (s_IDdoituong!="") sql+=" and dt.id='"+s_IDdoituong+"'";
			sql+=" group by nvl(kp.makp,'---'),nvl(kp.tenkp,'---'),nvl(upper(dt.mavt),'KHAC'),nvl(dt.tendt,'DT Khac'),";
			sql+=" sd.madt,dt.diachi,dt.dienthoai,dt.email,dt.fax,to_date('"+s_ddmmyyyy+"','dd/mm/yyyy'),";
			sql+=" to_date('"+s_ddmmyyyy+"','dd/mm/yyyy'),'So du dau ky',0,0,'',0,1";
			sql+=" order by nvl(kp.makp,'---'),nvl(dt.tendt,'DT Khac')";

			try
			{
				ds_Sodu=a.get_data(sql).Copy();
				if (ds_Sodu==null || ds_Sodu.Tables[0].Rows.Count<=0) return null;
				else return ds_Sodu;
			}
			catch
			{
				return null;
			}
		}

		//Tinh so du no, so du co cho cac doi tuong.
		private System.Data.DataSet f_Laysodu_doituong(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDhoatdong,string s_IDnhombc,string s_mm,string s_yy,string s_yyyy)
		{
			System.Data.DataSet ds_Sodu = new DataSet();
			//Tinh so du dau ky cho tat ca doi tuong.
			sql=  "select distinct '"+s_yyyy+"' as thangg,sd.madt as iddt,nvl(upper(dt.mavt),'KHAC') as madt,nvl(dt.tendt,'DT Khac') as tendt,0 as sotienno,";
			sql+=" 0 as sotienco,sum(sdno"+s_mm+") as tondauno, sum(sdco"+s_mm+") as tondauco,0 as cuoikyno, 0 as cuoikyco,";
			sql+=" 0 as loaictu, 1 as sapxep";
			sql+=" from "+a.user+".a_sd"+s_yy+" sd left join "+a.user+".a_dmdt dt on sd.madt=dt.id";
			sql+=" where sd.stka in ("+s_IDtk+")";
			if (s_IDchuong!="") sql+=" and sd.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and sd.lk="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and sd.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and sd.idbc="+s_IDnhombc;
			sql+=" group by '"+s_yyyy+"',sd.madt,nvl(upper(dt.mavt),'KHAC'),nvl(dt.tendt,'DT Khac'),0,0,0,0,0,1";
			sql+=" order by nvl(dt.tendt,'DT Khac')";

			try
			{
				ds_Sodu=a.get_data(sql);//.Copy();
				if (ds_Sodu==null || ds_Sodu.Tables[0].Rows.Count<=0) return null;
				else return ds_Sodu;
			}
			catch
			{
				return null;
			}
		}

		//Bao cao Tong hop cong no.
		public System.Data.DataSet f_Tonghopcongno(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_yy,string s_yyyy,bool b_Checkngay,string s_IDdoituong,string s_muc,string s_IDkm,string s_IDkm2,string s_IDkm3,string s_IDkm4,int i_dkgroup)
		{
			System.Data.DataRow dr_Data;
			flag_dsnull=false;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";			

			//Lay chung tu trong ky.
			sql=  "select distinct to_char(ct.ngayg,'yyyy') as thangg,dt.id as iddt,nvl(upper(dt.mavt),'KHAC') as madt,nvl(dt.tendt,'DT Khac') as tendt,";
			sql+=" sum(case when ctct.tknoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienno,";
			sql+=" sum(case when ctct.tkcoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienco,";
			sql+=" hd.ten as hoatdong,ch.ten as chuong,dt.tendt as doituong,bc.ten as nhombc,";
			sql+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
			sql+=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
			sql+=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2, ";
			sql+=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
			sql+=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
			sql+=" 0 as tondauno,0 as tondauco,0 as cuoikyno,0 as cuoikyco,'' as ghichu,1 as loaictu,";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end) as sapxep";
			else sql+=" 2 as sapxep";
			sql+=" "+s_DKgroup+"";
			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
			sql+=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
			sql+=" left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
			sql+=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";
			sql+=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";

			sql+=" where ctct.tonghop=1";
		
			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
		
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and lk.id="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;
			sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
			if (s_IDdoituong!="") sql+=" and dt.id='"+s_IDdoituong+"'";
			if(s_muc!="")sql+=" and (chno.id in("+s_muc+") or chco.id in("+s_muc+"))";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";
			sql+=" group by to_char(ct.ngayg,'yyyy'),dt.id,nvl(upper(dt.mavt),'KHAC'),nvl(dt.tendt,'DT Khac'),0,0,0,0,'',1,";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end)";
			else sql+=" 2,ch.ten,hd.ten,dt.tendt,bc.ten,";
			sql+=" case when(kmno.ten <>' ') then kmno.ten else kmco.ten end,";
			sql+=" case when(kmno2.ten<>' ') then kmno2.ten else kmco2.ten end,";
			sql+=" case when(kmno3.ten<>' ' ) then kmno3.ten else kmco3.ten end,";
			sql+=" case when(kmno4.ten<>' ') then kmno4.ten else kmco4.ten end,";
			sql+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end";			

			sql+=" order by to_char(ct.ngayg,'yyyy'),nvl(dt.tendt,'DT Khac')";

			try 
			{ 
				ds_Data=a.get_data(sql).Copy(); 
				if (ds_Data.Tables[0].Rows.Count>0) flag_dsnull=false;
				else flag_dsnull=true;
			}
			catch { flag_dsnull=true; }

			//Tinh so du dau ky cho cac doi tuong.
			System.Data.DataSet ds_Sodu=new DataSet();
			if(f_Laysodu_doituong(a,s_IDchuong,s_IDtk,s_IDlk,s_IDhoatdong,s_IDnhombc,s_mm,s_yy,s_yyyy)!=null)
			{
				ds_Sodu=f_Laysodu_doituong(a,s_IDchuong,s_IDtk,s_IDlk,s_IDhoatdong,s_IDnhombc,s_mm,s_yy,s_yyyy).Copy();
				flag_dsnull=false;
			}
			else
				flag_dsnull=true;
			if (!flag_dsnull)
			{
				try
				{
					while (ds_Sodu.Tables[0].Select("","iddt").Length>0)
					{
						System.Data.DataRow dr=ds_Sodu.Tables[0].Select("","iddt")[0];
						string s_IDDoituong=dr["iddt"].ToString().Trim();

						try { dr_Data=ds_Data.Tables[0].Select("iddt='"+s_IDDoituong+"' and sapxep<>99","iddt")[0]; }
						catch { dr_Data=null; }
						if (dr_Data!=null)
						{
							dr_Data["tondauno"]=decimal.Parse(dr["tondauno"].ToString());
							dr_Data["tondauco"]=decimal.Parse(dr["tondauco"].ToString());
						}
						else
						{
							dr["loaictu"]=1;
							dr["sapxep"]=2;
							ds_Data.Tables[0].ImportRow(dr);
						}
						ds_Data.AcceptChanges();
						ds_Sodu.Tables[0].Rows.Remove(dr);
					}
					ds_Sodu.AcceptChanges();
					if (b_Checkngay) f_Tinh_congnodauthang(ref ds_Data);
				}
				catch {}
			}
//			else if(ds_Sodu.Tables[0].Rows.Count>0) 
//			{
//				ds_Data=ds_Sodu.Copy();
//				flag_dsnull=false;
//			}
			else flag_dsnull=true;
			if (!flag_dsnull)
				while (ds_Data.Tables[0].Select("loaictu<>3","iddt").Length>0)
				{
					System.Data.DataRow dr=ds_Data.Tables[0].Select("loaictu<>3","iddt")[0];
					if (decimal.Parse(dr["tondauno"].ToString())+decimal.Parse(dr["tondauco"].ToString())+decimal.Parse(dr["sotienno"].ToString())+decimal.Parse(dr["sotienco"].ToString())==0)
						ds_Data.Tables[0].Rows.Remove(dr);
					else
					{
						dr["cuoikyno"]=decimal.Parse(dr["tondauno"].ToString())+decimal.Parse(dr["sotienno"].ToString());
						dr["cuoikyco"]=decimal.Parse(dr["tondauco"].ToString())+decimal.Parse(dr["sotienco"].ToString());
						dr["loaictu"]=3;
					}
				}
			ds_Data.AcceptChanges();

			ds_Data.WriteXml("..\\xml\\KT_Tonghopcongno.xml",XmlWriteMode.WriteSchema);
			if (flag_dsnull) return null;
			else return ds_Data;
			//End tinh so du dau ky
		} 

		//So theo doi tam ung.
		public System.Data.DataSet f_Sotheodoitamung(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDhoatdong,string s_IDkhoaphong,string s_IDdoituong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_yy,string s_yyyy,bool b_Checkngay,string s_IDkm,string s_IDkm2,string s_IDkm3,string s_IDkm4,int i_dkgroup)
		{
			System.Data.DataRow dr_Data;
			flag_dsnull=false;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";		

			//Lay chung tu trong ky.
			sql=  "select nvl(kp.makp,'---') as makp,nvl(kp.tenkp,'---') as tenkp,nvl(upper(dt.mavt),'KHAC') as madt,";
			sql+=" nvl(dt.tendt,'DT Khac') as tendt,";
			sql+=" ct.madt as iddt,dt.diachi as diachidt,dt.dienthoai as dienthoaidt,dt.email as emaildt,dt.fax as faxdt,";
			sql+=" to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,ct.soctg,nvl(ctct.noidung,ct.noidung) as diengiai,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienno,";
			sql+=" (case when ctct.tkcoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienco,";
			sql+=" 0 as sodu,0 as fsnonam,0 as fsconam,'' as ghichu,";
			sql+=" hd.ten as hoatdong,ch.ten as chuong,dt.tendt as doituong,bc.ten as nhombc,";
			sql+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
			sql+=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
			sql+=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2, ";
			sql+=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
			sql+=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then 1 else 2 end) as loaictu,";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end) as sapxep";
			else sql+=" 2 as sapxep";
			sql+=" "+s_DKgroup+"";
			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
			sql+=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
			sql+=" left join "+a.user+".a_btdkp kp on dt.makp=kp.id";
			sql+=" left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
			sql+=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";
			sql+=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
			
			sql+=" where ctct.tonghop=1";
			
			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and lk.id="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;
			sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
			if (s_IDkhoaphong!="") sql+=" and dt.makp='"+s_IDkhoaphong+"'";
			if (s_IDdoituong!="") sql+=" and dt.id='"+s_IDdoituong+"'";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";
			sql+=" order by nvl(kp.makp,'---'),nvl(dt.tendt,'DT Khac'),to_char(ct.ngayg,'yyyymmdd'),ct.soctg";

			try 
			{ 
				ds_Data=a.get_data(sql).Copy(); 
				if (ds_Data.Tables[0].Rows.Count>0) flag_dsnull=false;
				else flag_dsnull=true;
			}
			catch { flag_dsnull=true; }

			System.Data.DataSet ds_Sodu=new DataSet();
			if(f_Laysodu_tamung(a,s_IDchuong,s_IDtk,s_IDlk,s_IDhoatdong,s_IDkhoaphong,s_IDdoituong,s_IDnhombc,s_mm,s_yy,s_yyyy)!=null)
			{
				ds_Sodu=f_Laysodu_tamung(a,s_IDchuong,s_IDtk,s_IDlk,s_IDhoatdong,s_IDkhoaphong,s_IDdoituong,s_IDnhombc,s_mm,s_yy,s_yyyy).Copy();
				flag_dsnull=false;
			}
			else
				flag_dsnull=true;

			//Tinh so du dau ky cho cac doi tuong.
			if (!flag_dsnull)
			{
				try
				{
					while (ds_Sodu.Tables[0].Select("","iddt").Length>0)
					{
						System.Data.DataRow dr=ds_Sodu.Tables[0].Select("","iddt")[0];
						string s_IDDoituong=dr["iddt"].ToString().Trim();

						try { dr_Data=ds_Data.Tables[0].Select("iddt='"+s_IDdoituong+"' and sapxep<>99","iddt")[0]; }
						catch { dr_Data=null; }
						if (dr_Data!=null)
						{
							dr_Data["sodu"]=decimal.Parse(dr["sodu"].ToString());
							dr_Data["fsnonam"]=decimal.Parse(dr["fsnonam"].ToString());
							dr_Data["fsconam"]=decimal.Parse(dr["fsconam"].ToString());
						}
						else
						{
							dr["loaictu"]=0;
							dr["sapxep"]=2;
							ds_Data.Tables[0].ImportRow(dr);
						}
						ds_Data.AcceptChanges();
						ds_Sodu.Tables[0].Rows.Remove(dr);
					}
					ds_Sodu.AcceptChanges();
					if (b_Checkngay) f_Tinh_tamungdauthang(ref ds_Data);
				}
				catch {}
			}			
			else flag_dsnull=true;
			//ds_Data.WriteXml("..\\xml\\KT_Sotheodoitamung.xml",XmlWriteMode.WriteSchema);
			if (flag_dsnull) return null;
			else return ds_Data;
			//End tinh so du dau ky
		} 

		//So chi tiet tai khoan.
		public System.Data.DataSet f_Sochitiettaikhoan(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_sotk, string s_IDlk,string s_IDhoatdong,string s_IDdoituong,string s_IDnhombc,int i_Tinhchat,string s_Tungay,string s_Denngay,string s_mm,int mm1,int mm2,string s_yy,string s_yyyy,bool b_Checkngay,string s_muc,string s_IDkm,string s_IDkm2,string s_IDkm3,string s_IDkm4,int i_dkgroup)
		{
			flag_dsnull=false;
			string s_mmyyyy="";
			decimal d_fsnongay=0, d_fscongay=0;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";

			//Lay chung tu trong ky.
			sql=  "select ct.thanggs as thangg,dt.id,dt.tendt,to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,ct.soctg,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,";
			sql+=" nvl(ctct.noidung,ct.noidung) as diengiai,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then dmtkco.stk else (case when";
			sql+=" ctct.tkcoa in("+s_IDtk+") then dmtkno.stk else '' end) end) as tkdoiung,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then dmtkno.stk else (case when";
			sql+=" ctct.tkcoa in("+s_IDtk+") then dmtkco.stk else '' end) end) as tkcon, '' tentkcon,";
			sql+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
			sql+=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
			sql+=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2, ";
			sql+=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
			sql+=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
			sql+=" hd.ten as hoatdong,ch.ten as chuong,dt.tendt as doituong,bc.ten as nhombc,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienno,";
			sql+=" (case when ctct.tkcoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienco,";
			sql+=" 0 as tondauno,0 as tondauco,0 as fsnoquy,0 as fscoquy,0 as fsnonam,0 as fsconam,'' as ghichu,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then 1 else 2 end) as loaictu,";
			if (b_Checkngay) 
			{
				sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy'))";
				sql+=" then 99 else to_number(to_char(ct.ngayg,'yyyymm')||'2') end) as sapxep";
			}
			else sql+=" to_number(to_char(ct.ngayg,'yyyymm')||'2') as sapxep";
			sql+=" "+s_DKgroup+"";
			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmtk dmtkno on ctct.tknoa=dmtkno.id";
			sql+=" left join "+a.user+".a_dmtk dmtkco on ctct.tkcoa=dmtkco.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
			sql+=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
			sql+=" left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
			sql+=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";
			sql+=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
			sql+=" where ctct.tonghop=1";
			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";				
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and lk.id="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;
			sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
			if (s_IDdoituong!="") sql+=" and dt.id='"+s_IDdoituong+"'";
			if(s_muc!="")sql+=" and (chno.id in("+s_muc+") or chco.id in("+s_muc+"))";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";	
			sql+=" order by to_char(ct.ngayg,'yyyymmdd'),to_char(ct.ngaygs,'yyyymmdd'),ct.soctg";

			try 
			{ 
				ds_Data=a.get_data(sql).Copy(); 
				//				if (ds_Data.Tables[0].Rows.Count>0) flag_dsnull=false;
				//				else flag_dsnull=true;
			}
			catch { flag_dsnull=true; }

			//Tinh so du va luy ke dau ky.
			if (b_Checkngay) f_Tinh_chungtudauthang(ref ds_Data,"sotienno","sotienco","sapxep","99","ngayg",ref d_fsnongay,ref d_fscongay);
			try
			{
				decimal d_Daukyno=0,d_Daukyco=0,d_FSquyno=0,d_FSquyco=0,d_FSnamno=0,d_FSnamco=0,sumAll=0;
				//Them dong load so du dau ky.
				string[] id_tk=s_IDtk.Split(',');
				for(int k=0;k<id_tk.Length;k++)
				{
					string sd_tk=id_tk[k].ToString();
					for (int j=mm1;j<=mm2;j++)
					{
						try
						{ 
							System.Data.DataRow drSodu = ds_Data.Tables[0].NewRow();
							drSodu["thangg"]=j.ToString().PadLeft(2,'0')+"/"+s_yyyy.ToString().PadLeft(4,'0');
							drSodu["id"]=0;
							drSodu["tendt"]="";
							drSodu["ngaygs"]=DateTime.Parse(j.ToString().PadLeft(2,'0')+"/01/"+s_yyyy.ToString());
							drSodu["soctg"]="";
							drSodu["ngayg"]=DateTime.Parse(j.ToString().PadLeft(2,'0')+"/01/"+s_yyyy.ToString());
							drSodu["diengiai"]="Số dư đầu tháng "+j.ToString().PadLeft(2,'0');
							drSodu["tkdoiung"]="";
							drSodu["tkcon"]=a.get_stk_id(int.Parse(sd_tk.ToString()));
							drSodu["tentkcon"]=a.ttk_id(int.Parse(sd_tk.ToString()));

							//Tham so cuoi: 1-lay so du no hoac phat sinh no, 2-lay so du co hoac phat sinh co.
							drSodu["sotienno"]=0;
							drSodu["sotienco"]=0;
						
							drSodu["ghichu"]="";
							drSodu["loaictu"]=0;
							drSodu["sapxep"]=int.Parse(s_yyyy.ToString()+j.ToString().PadLeft(2,'0')+"1");						

							int CT_Quy = ((j-1)/3)*3+1;
							if(j==mm1)
							{
								d_Daukyno=a.get_Sodudauky_scttk(j.ToString().PadLeft(2,'0'),s_yy,sd_tk,s_IDlk,s_IDdoituong,1)+d_fsnongay;
								d_Daukyco=a.get_Sodudauky_scttk(j.ToString().PadLeft(2,'0'),s_yy,sd_tk,s_IDlk,s_IDdoituong,2)+d_fscongay;

								drSodu["fsconam"]=a.get_FSluykenam_quy_scttk(s_yy,sd_tk,s_IDlk,2,1,mm1,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4);//b_CheckChuong,s_IDChuong,b_CheckHoatDong,s_IDHoatDong,s_IDNhombc,b_CheckNhombc);
								drSodu["fsnonam"]=a.get_FSluykenam_quy_scttk(s_yy,sd_tk,s_IDlk,1,1,mm1,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4);//b_CheckChuong,s_IDChuong,b_CheckHoatDong,s_IDHoatDong,s_IDNhombc,b_CheckNhombc);

								drSodu["fscoquy"]=a.get_FSluykenam_quy_scttk(s_yy,sd_tk,s_IDlk,2,CT_Quy,mm1,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4);//b_CheckChuong,s_IDChuong,b_CheckHoatDong,s_IDHoatDong,s_IDNhombc,b_CheckNhombc);
								drSodu["fsnoquy"]=a.get_FSluykenam_quy_scttk(s_yy,sd_tk,s_IDlk,1,CT_Quy,mm1,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4);//b_CheckChuong,s_IDChuong,b_CheckHoatDong,s_IDHoatDong,s_IDNhombc,b_CheckNhombc);
							}
							else
							{	
								s_mmyyyy=(j-1).ToString().PadLeft(2,'0');//+"/"+s_yyyy;
								decimal d_Luyke_ds_no=a.get_FSluyke_ds("thangg='"+s_mmyyyy+"'",ds_Data,"sotienno","sotienco",1);
								decimal d_Luyke_ds_co=a.get_FSluyke_ds("thangg='"+s_mmyyyy+"'",ds_Data,"sotienno","sotienco",2);

								switch (i_Tinhchat)
								{
									case 0:
										d_Daukyno=(d_Daukyno+d_Luyke_ds_no) - Math.Min((d_Daukyno+d_Luyke_ds_no),(d_Daukyco+d_Luyke_ds_co));
										d_Daukyco=(d_Daukyco+d_Luyke_ds_co) - Math.Min((d_Daukyno+d_Luyke_ds_no),(d_Daukyco+d_Luyke_ds_co));
										break;
									case 1:
										d_Daukyno=d_Daukyno+d_Luyke_ds_no-d_Luyke_ds_co;
										d_Daukyco=0;
										break;
									case 2:
										d_Daukyno=0;
										d_Daukyco=d_Daukyco+d_Luyke_ds_co-d_Luyke_ds_co;
										break;
								}
							
								d_FSquyno+=d_Luyke_ds_no;
								d_FSquyco+=d_Luyke_ds_co;

								d_FSnamno+=d_Luyke_ds_no;
								d_FSnamco+=d_Luyke_ds_co;

								sumAll+=d_Daukyno+d_Daukyco+d_FSquyno+d_FSquyco+d_FSnamno+d_FSnamco;
								drSodu["fsconam"]=a.get_FSluykenam_quy_scttk(s_yy,sd_tk,s_IDlk,2,1,j,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4);//b_CheckChuong,s_IDChuong,b_CheckHoatDong,s_IDHoatDong,s_IDNhombc,b_CheckNhombc);
								drSodu["fsnonam"]=a.get_FSluykenam_quy_scttk(s_yy,sd_tk,s_IDlk,1,1,j,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4);//b_CheckChuong,s_IDChuong,b_CheckHoatDong,s_IDHoatdong,s_IDNhombc,b_CheckNhombc);
								drSodu["fscoquy"]=a.get_FSluykenam_quy_scttk(s_yy,sd_tk,s_IDlk,2,CT_Quy,j,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4);//b_CheckChuong,s_IDChuong,b_CheckHoatDong,s_IDHoatDong,s_IDNhombc,b_CheckNhombc);
								drSodu["fsnoquy"]=a.get_FSluykenam_quy_scttk(s_yy,sd_tk,s_IDlk,1,CT_Quy,j,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4);//b_CheckChuong,s_IDChuong,b_CheckHoatDong,s_IDHoatDong,s_IDNhombc,b_CheckNhombc);
							}						

							drSodu["tondauno"]=d_Daukyno;
							drSodu["tondauco"]=d_Daukyco;

							ds_Data.Tables[0].Rows.Add(drSodu);
							ds_Data.AcceptChanges();
						}					
						catch{flag_dsnull=true;}	;
					}
				}
			}
			catch{flag_dsnull=true;};
			ds_Data.WriteXml("..\\xml\\KT_Soctcactk.xml",XmlWriteMode.WriteSchema);
			if (flag_dsnull) return null;
			else return ds_Data;
			//End tinh so du dau ky
		} 

		//So chi tiet tai khoan_mau2
		public System.Data.DataSet f_Sochitiettaikhoan_m2(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDhoatdong,string s_IDdoituong,string s_IDnhombc,int i_Tinhchat,string s_Tungay,string s_Denngay,string s_mm,int mm1,int mm2,string s_yy,string s_yyyy,bool b_Checkngay,string s_muc,string s_IDkm,string s_IDkm2,string s_IDkm3,string s_IDkm4,int i_dkgroup)
		{
			string sql_sd="",sql_lk="";
			
			flag_dsnull=false;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";
			//chung tu fs
			//sql=" select t1.*,t2.sddaukyno,t2.sddaukyco,t3.fsluykeno,t3.fsluykeco from(";
			sql=  "select ct.thanggs as thangg,dt.id,dt.tendt,to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,ct.soctg,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,";
			sql+=" nvl(ctct.noidung,ct.noidung) as diengiai,(case when ctct.tknoa in("+s_IDtk+") then dmtkco.stk else (case when";
			sql+=" ctct.tkcoa in("+s_IDtk+") then dmtkno.stk else '' end) end) as tkdoiung,";
			sql+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
			sql+=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
			sql+=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2, ";
			sql+=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
			sql+=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
			sql+=" hd.ten as hoatdong,ch.ten as chuong,dt.tendt as doituong,bc.ten as nhombc,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienno,";
			sql+=" (case when ctct.tkcoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienco,";
			sql+=" 0 as sddaukyno, 0 as sddaukyco, 0 as fsluykeno,0 as fsluykeco,'' as ghichu,";
			//sql+=" '' as ghichu,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then 1 else 2 end) as loaictu,";
			if (b_Checkngay) 
			{
				sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy'))";
				sql+=" then 99 else to_number(to_char(ct.ngayg,'yyyymm')||'2') end) as sapxep";
			}
			else sql+=" to_number(to_char(ct.ngayg,'yyyymm')||'2') as sapxep";
			sql+=" "+s_DKgroup+"";
			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmtk dmtkno on ctct.tknoa=dmtkno.id";
			sql+=" left join "+a.user+".a_dmtk dmtkco on ctct.tkcoa=dmtkco.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
			sql+=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
			sql+=" left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
			sql+=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";
			sql+=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
			sql+=" where ctct.tonghop=1";
			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and lk.id="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;
			sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
			if (s_IDdoituong!="") sql+=" and dt.id='"+s_IDdoituong+"'";
			if(s_muc!="")sql+=" and (chno.id in("+s_muc+") or chco.id in("+s_muc+"))";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";	
			sql+=" order by to_char(ct.ngayg,'yyyymmdd'),to_char(ct.ngaygs,'yyyymmdd'),ct.soctg";
//			tinh sodu dau ky
//			sql+=")t1 left join ";
//			sql+=" ("+ a.get_Sodudauky_dt_m2(mm1.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,s_IDdoituong,1,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4)+")t2";
//			sql+=" on t1.id=t2.id";
//			tinh fs luyke
//			sql+=" left join ";
//			sql+= "("+ a.get_FSluykenam_dt_m2(mm1.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,s_IDdoituong,1,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4)+")t3 ";
//			sql+=" on t1.id=t3.id";


			ds_Data=a.get_data(sql);

			//tinh so du dau ky


//			//tinh sodu dauky
			DataSet ds_Datasd=new DataSet();
			sql_sd=a.get_Sodudauky_dt_m2(mm1.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,s_IDdoituong,1,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4,i_dkgroup);
			ds_Datasd=a.get_data(sql_sd);
			if(ds_Data!=null||ds_Datasd!=null)
				ds_Data.Merge(ds_Datasd);
			ds_Data.AcceptChanges();

			ds_Data.WriteXml("..\\xml\\KT_Soctcactksd_m2.xml",XmlWriteMode.WriteSchema);
			//tinh fs luy ke

			DataSet ds_datalk=new DataSet();
			sql_lk=a.get_FSluykenam_dt_m2(mm1.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,s_IDdoituong,1,s_IDchuong,s_IDhoatdong,s_IDnhombc,s_muc,s_IDkm,s_IDkm2,s_IDkm3,s_IDkm4,i_dkgroup);
			ds_datalk=a.get_data(sql_lk);
			if(ds_Data!=null||ds_datalk!=null)
				ds_Data.Merge(ds_datalk);
			ds_Data.AcceptChanges();	
	
			if(ds_Data!=null)
			ds_Data.WriteXml("..\\xml\\KT_Soctcactk_m2.xml",XmlWriteMode.WriteSchema);
			if (flag_dsnull) return null;
			else return ds_Data;
		} 

		//So tien gui ngan hang - chi tiet (S08 - DN)
		public System.Data.DataSet f_Sotienguinganhang(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDkm,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_yy,string s_yyyy,bool b_Checkngay,string s_IDdoituong,string s_muc,string s_IDkm2,string s_IDkm3,string s_IDkm4,int i_dkgroup)
		{
			decimal d_fsnongay=0, d_fscongay=0;
			flag_dsnull=false;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;

			string s_DKSelectgroup="",s_DKfrom="";

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";

			switch(i_dkgroup)
			{
				case 0:
					s_DKSelectgroup=" ch.ten as chuong,ch.id as idchuong,";
					s_DKSelectgroup+=" '' as iddoituong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as doituong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";					
					break;
				case 1:
					s_DKSelectgroup=" dt.tendt as doituong,dt.id as iddoituong,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					break;
				case 2:
					s_DKSelectgroup=" hd.ten as hoatdong,hd.id as idhoatdong,";
					s_DKSelectgroup+=" '' as idchuong,'' as iddoituong, '' as idnhombc,'' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong,'' as doituong, '' as nhombc,'' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
					break;
				case 3:
					s_DKSelectgroup=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
					s_DKSelectgroup+=" case when(chno.ttk <>' ') then chno.id else chco.id end as idmuc,";
					s_DKSelectgroup+=" '' as idchuong,'' as iddoituong, '' as idnhombc,'' as idhoatdong, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong,'' as doituong, '' as nhombc,'' as hoatdong, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
					s_DKfrom+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
					break;
				case 4:
					s_DKSelectgroup=" bc.ten as nhombc,bc.id as idnhombc,";
					s_DKSelectgroup+=" '' as idchuong, '' as iddoituong,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as doituong,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKfrom="left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
					break;
				case 5:
					s_DKSelectgroup=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
					s_DKSelectgroup+=" case when (kmno.ten <>' ') then kmno.id else kmco.id end as idkm,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as iddoituong, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as doituong, '' as km2, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
					break;
				case 6:
					s_DKSelectgroup=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2,";
					s_DKSelectgroup+=" case when (kmno2.ten <>' ') then kmno2.id else kmco2.id end as idkm2,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as iddoituong, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as doituong, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
					break;
				case 7:
					s_DKSelectgroup=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
					s_DKSelectgroup+=" case when (kmno3.ten<>' ') then kmno3.id else kmco3.id end as idkm3,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as iddoituong, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as doituong, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
					break;
				case 8:
					s_DKSelectgroup=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
					s_DKSelectgroup+=" case when (kmno4.ten<>' ') then kmno4.id else kmco4.id end as idkm4,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as iddoituong,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as doituong,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";	
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
					break;
				default: 					
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,'' as iddoituong,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,'' as doituong,";
					break;
			}
			//

			sql=  "select to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,ct.soctg as sohieu,nvl(dt.tendt,ctk.tendt) as hoten,";
			sql+=" 0 as sodu_no,0 as sodu_co,0 as fsluyke_no, 0 as fsluyke_co,";
			//sql+=" nvl(ctct.noidung,ct.noidung) diengiai,(case when(ctct.tonghop=1 and ctct.tknoa in("+s_IDtk+")) then dmtkco.stk";
			sql+=" "+s_DKSelectgroup+"";
			sql+=" nvl(ctct.noidung,ct.noidung) as diengiai,(case when ctct.tknoa in("+s_IDtk+")then dmtkco.stk";
			sql+=" else (case when ctct.tkcoa in("+s_IDtk+")then dmtkno.stk else '' end) end) as tkdoiung,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+")then ctct.stien else 0 end) as sotienthu,(case when";
			sql+=" ctct.tkcoa in("+s_IDtk+")then ctct.stien else 0 end) as sotienchi,0 as stton,0 as fsnothtruoc,0 as fscothtruoc,'' as ghichu,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then 1 else 2 end) as loaictu,";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end) as sapxep";
			else sql+=" 2 as sapxep";
			sql+=" "+s_DKgroup+"";
			sql+=" from "+a.user+".a_ct"+s_yy+" ct left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_ctk"+s_yy+" ctk on ct.id=ctk.id";
			sql+=" left join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmtk dmtkno on ctct.tknoa=dmtkno.id";
			sql+=" left join "+a.user+".a_dmtk dmtkco on ctct.tkcoa=dmtkco.id";
			sql+=" "+s_DKfrom+"";
			
			sql+=" where ctct.tonghop=1";
			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
			
			sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and lk.id="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;

			if(s_IDdoituong!="")sql+=" and dt.id="+s_IDdoituong;
			if(s_muc!="")sql+=" and (ctct.mtmno in("+s_muc+") or ctct.mtmco in("+s_muc+"))";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";	
			//			sql+=" group by ct.ngaygs,ct.ngayg,ct.soctg,";
			//			sql+=" nvl(dt.tendt,ctk.tendt),nvl(ctct.noidung,ct.noidung),(case when (nghang.maso='PT' and ctct.tknoa in("+s_IDtk+"))";
			//			sql+=" then dmtkco.stk else (case when (nghang.maso='PC' and ctct.tkcoa in("+s_IDtk+")) then dmtkno.stk else '' end) end),";
			//			sql+=" 0,0,0,'',(case when (nghang.maso='PT' and ct.tknoa in("+s_IDtk+")) then 1 else 2 end),2";
			sql+=" order by to_char(ct.ngaygs,'yyyymmdd'),to_char(ct.ngayg,'yyyymmdd')";

			try
			{
				ds_Data=a.get_data(sql).Copy();
				if (ds_Data==null || ds_Data.Tables[0].Rows.Count<=0) flag_dsnull=true;
				else
				{
					if(i_dkgroup!=-1)
					{
						DataSet ds_Data2=new DataSet();
						ds_Data2=a.get_dsSodudauky(s_mm,s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4,i_dkgroup);
						if(ds_Data2!=null)
							ds_Data.Merge(ds_Data2);
						ds_Data.AcceptChanges();
						for (int i=1;i<int.Parse(s_mm);i++)
						{
							DataSet ds_Data3=new DataSet();
							ds_Data3=a.get_FSluykenamdt(s_mm,s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,i_dkgroup,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4,i);
						
							if(ds_Data3!=null)
								ds_Data.Merge(ds_Data3);
						}
						ds_Data.AcceptChanges();
					}
				}
			}
			catch
			{
				flag_dsnull=true;
			}
			//Tinh luy ke tu dau thang den ngay hien tai.
			//ds_Data.WriteXml("..\\xml\\KT_Sotienguinganhang.xml",XmlWriteMode.WriteSchema);
			if (b_Checkngay) f_Tinh_chungtudauthang(ref ds_Data,"sotienthu","sotienchi","sapxep","99","ngayg",ref d_fsnongay,ref d_fscongay);

			try
			{
				decimal d_Dauky=0,d_FSnamno=0,d_FSnamco=0,sumAll=0;
				//Them dong load so du dau ky.
			
				System.Data.DataRow dr_Sodu = ds_Data.Tables[0].NewRow();
				dr_Sodu["ngaygs"]="01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy;//DateTime.ParseExact(,"dd/MM/yyyy",null);
				dr_Sodu["ngayg"]="01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy;//DateTime.ParseExact(,"dd/MM/yyyy",null);
				dr_Sodu["sohieu"]="";
				dr_Sodu["hoten"]="";
				dr_Sodu["diengiai"]="Số dư đầu kỳ";
				dr_Sodu["tkdoiung"]="";
				dr_Sodu["sotienthu"]=0;
				dr_Sodu["sotienchi"]=0;

				d_Dauky=a.get_Sodudauky(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,3,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+(d_fsnongay-d_fscongay);
				d_FSnamno=a.get_FSluykenam(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+d_fsnongay;
				d_FSnamco=a.get_FSluykenam(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+d_fscongay;
				sumAll=d_Dauky+d_FSnamno+d_FSnamco;

				dr_Sodu["stton"]=d_Dauky;
				dr_Sodu["fsnothtruoc"]=d_FSnamno;
				dr_Sodu["fscothtruoc"]=d_FSnamco;
				dr_Sodu["ghichu"]="";
				dr_Sodu["loaictu"]=0;
				dr_Sodu["sapxep"]=1;

				ds_Data.Tables[0].Rows.Add(dr_Sodu);
				ds_Data.AcceptChanges();
				if (flag_dsnull && sumAll==0) flag_dsnull=true;
				else flag_dsnull=false;
			}
			catch{};
			if (flag_dsnull) return null;
			else return ds_Data;
			//End tinh so du dau ky
		} 

		//So tien gui ngan hang - tong hop (S08 - DN)
		public System.Data.DataSet f_Sotienguinganhang_th(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDkm,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_yy,string s_yyyy,bool b_Checkngay,string s_IDdoituong,string s_muc,string s_IDkm2,string s_IDkm3,string s_IDkm4,int i_dkgroup)
		{
			decimal d_fsnongay=0, d_fscongay=0;
			flag_dsnull=false;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;

			//
			string s_DKSelectgroup="",s_DKGroupbygroup="",s_DKfrom="";

			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";

			switch(i_dkgroup)
			{
				case 0:
					s_DKSelectgroup=" ch.ten as chuong,ch.id as idchuong,";
					s_DKSelectgroup+=" '' as iddoituong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as doituong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" ch.ten,ch.id,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";					
					break;
				case 1:
					s_DKSelectgroup=" dt.tendt as doituong,dt.id as iddoituong,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" dt.tendt,dt.id,";									
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					break;
				case 2:
					s_DKSelectgroup=" hd.ten as hoatdong,hd.id as idhoatdong,";
					s_DKSelectgroup+=" '' as idchuong,'' as iddoituong, '' as idnhombc,'' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong,'' as doituong, '' as nhombc,'' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" hd.ten,hd.id,";
					s_DKfrom=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					break;
				case 3:
					s_DKSelectgroup=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
					s_DKSelectgroup+=" case when(chno.ttk <>' ') then chno.id else chco.id end as idmuc,";
					s_DKSelectgroup+=" '' as idchuong,'' as iddoituong, '' as idnhombc,'' as idhoatdong, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong,'' as doituong, '' as nhombc,'' as hoatdong, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" case when(chno.ttk <>' ') then chno.id else chco.id end,";
					s_DKGroupbygroup+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
					s_DKfrom+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
					break;
				case 4:
					s_DKSelectgroup=" bc.ten as nhombc,bc.id as idnhombc,";
					s_DKSelectgroup+=" '' as idchuong, '' as iddoituong,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as doituong,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" bc.ten,bc.id,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom="left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
					break;
				case 5:
					s_DKSelectgroup=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
					s_DKSelectgroup+=" case when (kmno.ten <>' ') then kmno.id else kmco.id end as idkm,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as iddoituong, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as doituong, '' as km2, '' as km3, '' as km4,";
					s_DKGroupbygroup=" case when(kmno.ten <>' ') then kmno.ten else kmco.ten end,";
					s_DKGroupbygroup+=" case when(kmno.ten <>' ') then kmno.id else kmco.id end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
					break;
				case 6:
					s_DKSelectgroup=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2,";
					s_DKSelectgroup+=" case when (kmno2.ten <>' ') then kmno2.id else kmco2.id end as idkm2,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as iddoituong, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as doituong, '' as km3, '' as km4,";
					s_DKGroupbygroup=" case when(kmno2.ten<>' ') then kmno2.ten else kmco2.ten end,";
					s_DKGroupbygroup+=" case when(kmno2.ten<>' ') then kmno2.id else kmco2.id end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
					break;
				case 7:
					s_DKSelectgroup=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
					s_DKSelectgroup+=" case when (kmno3.ten<>' ') then kmno3.id else kmco3.id end as idkm3,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as iddoituong, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as doituong, '' as km4,";
					s_DKGroupbygroup=" case when(kmno3.ten<>' ' ) then kmno3.ten else kmco3.ten end,";
					s_DKGroupbygroup+=" case when(kmno3.ten<>' ' ) then kmno3.id else kmco3.id end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
					break;
				case 8:
					s_DKSelectgroup=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
					s_DKSelectgroup+=" case when (kmno4.ten<>' ') then kmno4.id else kmco4.id end as idkm4,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as iddoituong,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as doituong,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";	
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";s_DKGroupbygroup=" case when(kmno4.ten<>' ') then kmno4.ten else kmco4.ten end,";
					s_DKGroupbygroup+=" case when(kmno4.ten<>' ') then kmno4.id else kmco4.id end,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					break;
				default: 					
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,'' as iddoituong,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,'' as doituong,";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					s_DKGroupbygroup+=" '', '', '', '', '', '', '','',";
					break;
			}
			//
			
			sql=  "select to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,ct.soctg as sohieu,nvl(dt.tendt,ctk.tendt) as hoten,";
			sql+=" nvl(ct.noidung,'Chưa xác định')as diengiai,'000' as tkdoiung,";
			sql+=" 0 as sodu_no,0 as sodu_co,0 as fsluyke_no, 0 as fsluyke_co,";
			sql+=" "+s_DKSelectgroup+"";
			sql+=" sum(case when (ct.tknoa in("+s_IDtk+"))then ctct.stien else 0 end) as sotienthu,sum(case when";
			sql+=" (ct.tkcoa in("+s_IDtk+"))then ctct.stien else 0 end) as sotienchi,0 as stton,0 as fsnothtruoc,0 as fscothtruoc,'' as ghichu,";
			sql+=" (case when (ctct.tknoa in("+s_IDtk+"))then 1 else 2 end) as loaictu,";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end) as sapxep";
			else sql+=" 2 as sapxep";
			sql+=" "+s_DKgroup+"";
			sql+=" from "+a.user+".a_ct"+s_yy+" ct left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" left join "+a.user+".a_ctk"+s_yy+" ctk on ct.id=ctk.id";
			sql+=" left join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" "+s_DKfrom+"";

			sql+=" where ct.chucnang=2";
			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
			
			sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
			
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and lk.id="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;

			if(s_IDdoituong!="")sql+=" and dt.id="+s_IDdoituong;
			if(s_muc!="")sql+=" and (ctct.mtmno in("+s_muc+") or ctct.mtmco in("+s_muc+"))";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";

			sql+=" group by ct.ngaygs,ct.ngayg,ct.soctg,";
			sql+=" nvl(dt.tendt,ctk.tendt),nvl(ct.noidung,'Chưa xác định'),'000',";
			sql+=" 0,0,0,'',(case when (ctct.tknoa in("+s_IDtk+")) then 1 else 2 end),";
			sql+=" "+s_DKGroupbygroup+"";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end)";
			else sql+=" 2";
			sql+=" order by to_char(ct.ngaygs,'yyyymmdd'),to_char(ct.ngayg,'yyyymmdd')";


			try
			{
				ds_Data=a.get_data(sql).Copy();
				if (ds_Data==null || ds_Data.Tables[0].Rows.Count<=0) flag_dsnull=true;
				else
				{
					if(i_dkgroup!=-1)
					{
						DataSet ds_Data2=new DataSet();
						ds_Data2=a.get_dsSodudauky(s_mm,s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4,i_dkgroup);
						if(ds_Data2!=null)
							ds_Data.Merge(ds_Data2);
						ds_Data.AcceptChanges();
						for (int i=1;i<int.Parse(s_mm);i++)
						{
							DataSet ds_Data3=new DataSet();
							ds_Data3=a.get_FSluykenamdt(s_mm,s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,i_dkgroup,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4,i);
						
							if(ds_Data3!=null)
								ds_Data.Merge(ds_Data3);
						}
						ds_Data.AcceptChanges();
					}
				}
			}
			catch
			{
				flag_dsnull=true;
			}
			//Tinh luy ke tu dau thang den ngay hien tai.
			ds_Data.WriteXml("..\\xml\\KT_Sotienguinganhang_th.xml",XmlWriteMode.WriteSchema);
			if (b_Checkngay) f_Tinh_chungtudauthang(ref ds_Data,"sotienthu","sotienchi","sapxep","99","ngayg",ref d_fsnongay,ref d_fscongay);

			try
			{
				decimal d_Dauky=0,d_FSnamno=0,d_FSnamco=0,sumAll=0;
				//Them dong load so du dau ky.
			
				System.Data.DataRow dr_Sodu = ds_Data.Tables[0].NewRow();
				dr_Sodu["ngaygs"]="01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy;//DateTime.ParseExact(,"dd/MM/yyyy",null);
				dr_Sodu["ngayg"]="01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy;//DateTime.ParseExact(,"dd/MM/yyyy",null);
				dr_Sodu["sohieu"]="";
				dr_Sodu["hoten"]="";
				dr_Sodu["diengiai"]="Số dư đầu kỳ";
				dr_Sodu["tkdoiung"]="";
				dr_Sodu["sotienthu"]=0;
				dr_Sodu["sotienchi"]=0;

				d_Dauky=a.get_Sodudauky(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,3,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+(d_fsnongay-d_fscongay);
				d_FSnamno=a.get_FSluykenam(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+d_fsnongay;
				d_FSnamco=a.get_FSluykenam(s_mm.ToString().PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4)+d_fscongay;
				sumAll=d_Dauky+d_FSnamno+d_FSnamco;

				dr_Sodu["stton"]=d_Dauky;
				dr_Sodu["fsnothtruoc"]=d_FSnamno;
				dr_Sodu["fscothtruoc"]=d_FSnamco;
				dr_Sodu["ghichu"]="";
				dr_Sodu["loaictu"]=0;
				dr_Sodu["sapxep"]=1;

				ds_Data.Tables[0].Rows.Add(dr_Sodu);
				ds_Data.AcceptChanges();
				if (flag_dsnull && sumAll==0) flag_dsnull=true;
				else flag_dsnull=false;
			}
			catch{};
			if (flag_dsnull) return null;
			else return ds_Data;
			//End tinh so du dau ky
		} 

		private System.Data.DataSet f_Socai_ct_muc_datablank (LibKetoan.AccessData a)
		{
			sql=  "select '01/2008' as thangg,'000' as nhommuc,to_date('01/01/2008','dd/mm/yyyy') as ngaygs,'' as soctg,";
			sql+=" to_date('01/01/2008','dd/mm/yyyy') as ngayg,'' as diengiai,'' as tkdoiung,'000' as muc,0 as sotienno,0 as sotienco,";
			sql+=" 0 as tondauno,0 as tondauco,0 as tondaumucno,0 as tondaumucco,0 as fsnoquy,0 as fscoquy,0 as fsnonam,0 as fsconam,'' as ghichu,";
			sql+=" 3 as loaictu,0 as sapxep from "+a.user+".dual";
			ds_Data=new System.Data.DataSet();
			try
			{
				ds_Data=a.get_data(sql).Copy();
			}
			catch{}	;
			return ds_Data;
		}

		

		private System.Data.DataSet f_Socai_ct_muc_sodu(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDmuc,string s_IDhoatdong,string s_IDnhombc,string s_mm,string s_yy,string s_yyyy)
		{
			//Lay so du dau ky, group theo tung muc.
			flag_dsnull=false;
			string s_Thangg=s_mm+"/"+s_yyyy;
			try
			{
				sql=  "select distinct '"+s_Thangg+"' as thangg,nvl(substr(trim(dmch.stk),1,3),'000') as nhommuc,";
				sql+=" to_date('01/"+s_Thangg+"','dd/mm/yyyy') as ngaygs,' ' as soctg,to_date('01/"+s_Thangg+"','dd/mm/yyyy') as ngayg,";
				sql+=" 'So du dau ky theo muc' as diengiai,' ' as tkdoiung,'' as muc,0 as sotienno,0 as sotienco,0 as tondauno,";
				sql+=" 0 as tondauco,sum(sd.sdno"+s_mm+") as tondaumucno,";
				sql+=" sum(sd.sdco"+s_mm+") as tondaumucco,0 as fsnoquy,0 as fscoquy,";
				sql+=" 0 as fsnonam,0 as fsconam,' ' as ghichu,3 as loaictu,to_number('"+s_yyyy+s_mm+"'||'3') as sapxep";
				sql+=" from "+a.user+".a_sd"+s_yy+" sd left join "+a.user+".a_dmch dmch on sd.mtmuc=dmch.id";
				sql+=" where sd.stka in("+s_IDtk+")";
				if (s_IDchuong!="") sql+=" and sd.chuong='"+s_IDchuong+"'";
				if (s_IDlk!="") sql+=" and sd.lk='"+s_IDlk+"'";
				if (s_IDmuc!="") sql+=" and sd.mtmuc in("+s_IDmuc+")";
				if (s_IDhoatdong!="") sql+=" and sd.hoatdong='"+s_IDhoatdong+"'";
				if (s_IDnhombc!="") sql+=" and sd.idbc='"+s_IDnhombc+"'";
				sql+=" group by '"+s_Thangg+"',nvl(substr(trim(dmch.stk),1,3),'000'),to_date('01/"+s_Thangg+"','dd/mm/yyyy'),";
				sql+=" ' ',to_date('01/"+s_Thangg+"','dd/mm/yyyy'),'So du dau ky theo muc',' ','',0,0,0,0,0,0,0,0,' ',";
				sql+=" 3,to_number('"+s_yyyy+s_mm.PadLeft(2,'0')+"'||'3')";

				ds_Data=a.get_data(sql).Copy();
				if ((ds_Data==null || ds_Data.Tables[0].Rows.Count<=0)) flag_dsnull=true;
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Data;
		} 

		private System.Data.DataSet f_Tinhsolieucuoiky(System.Data.DataSet ds_MyData, int i_Tuthang, int i_Denthang)
		{
			//Void tinh cac so du, luy ke tung thang tren DataSet.
			//Nhan dang lam den day.


			//Add them row tu DataSet nay sang DataSet khac.
			System.Data.DataSet ds_Data_Socai;
			ds_Data_Socai=ds_MyData.Clone();
			System.Data.DataRow dr=ds_MyData.Tables[0].Rows[0];
			ds_Data_Socai.Tables[0].ImportRow(dr);
			ds_Data_Socai.AcceptChanges();
			return ds_Data_Socai;
		}

		public System.Data.DataSet f_Socai_ct_muc(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDmuc,string s_IDkm,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_yy,string s_yyyy,bool b_Checkngay,string s_IDdoituong,string s_IDkm2,string s_IDkm3,string s_IDkm4)
		{
			System.Data.DataSet ds_Socai_muc;
			//Lay so du dau ky va so du dau ky theo muc.
			ds_Socai_muc=f_Tinhsodu_luyke(a,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,s_mm,s_yy,s_yyyy,s_IDdoituong,s_IDmuc,s_IDkm2,s_IDkm3,s_IDkm4).Copy();
			ds_Socai_muc.Merge(f_Socai_ct_muc_sodu(a,s_IDchuong,s_IDtk,s_IDlk,s_IDmuc,s_IDhoatdong,s_IDnhombc,s_mm,s_yy,s_yyyy).Copy());
			//Lay phat sinh trong ky theo doi.
			flag_dsnull=false;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;
			try
			{
				sql=  "select distinct to_char(ct.ngayg,'mm/yyyy') as thangg,";
				if (s_IDmuc!="")
				{
					sql+=" (case when (ctct.tknoa in("+s_IDtk+") and ctct.mtmno in("+s_IDmuc+")) then substr(trim(dmchno.stk),1,3) else ";
					sql+=" (case when (ctct.tkcoa in("+s_IDtk+") and ctct.mtmco in("+s_IDmuc+")) then substr(trim(dmchco.stk),1,3) else '000' end) end) as nhommuc,";
				}
				else 
				{
					sql+=" (case when ctct.tknoa in("+s_IDtk+") then substr(trim(dmchno.stk),1,3) else ";
					sql+=" (case when ctct.tkcoa in("+s_IDtk+") then substr(trim(dmchco.stk),1,3) else '000' end) end) as nhommuc,";
				}
				sql+=" to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,ct.soctg,ct.ngayg as ngayg,";
				sql+=" nvl(ctct.noidung,ct.noidung) as diengiai,(case when ctct.tknoa in("+s_IDtk+") then dmtkco.stk else (case when";
				sql+=" ctct.tkcoa in("+s_IDtk+") then dmtkno.stk else '' end) end) as tkdoiung,";
				if (s_IDmuc!="")
				{
					sql+=" (case when (ctct.tknoa in("+s_IDtk+") and ctct.mtmno in("+s_IDmuc+")) then trim(dmchno.stk) else ";
					sql+=" (case when (ctct.tkcoa in("+s_IDtk+") and ctct.mtmco in("+s_IDmuc+")) then trim(dmchco.stk) else '' end) end) as muc,";
				}
				else 
				{
					sql+=" (case when ctct.tknoa in("+s_IDtk+") then trim(dmchno.stk) else ";
					sql+=" (case when ctct.tkcoa in("+s_IDtk+") then trim(dmchco.stk) else '' end) end) as muc,";
				}
				sql+=" (case when ctct.tknoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienno,";
				sql+=" (case when ctct.tkcoa in("+s_IDtk+") then ctct.stien else 0 end) as sotienco,";
				sql+=" 0 as tondauno,0 as tondauco,0 as tondaumucno,0 as tondaumucco,0 as fsnoquy,0 as fscoquy,0 as fsnonam,0 as fsconam,' ' as ghichu,";
				sql+=" (case when ctct.tknoa in("+s_IDtk+") then 1 else 2 end) as loaictu,";
				if (b_Checkngay) 
				{
					sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy'))";
					sql+=" then 99 else (to_number(to_char(ct.ngayg,'yyyymm')||'2')) end) as sapxep";
				}
				else sql+=" to_number(to_char(ct.ngayg,'yyyymm')||'2') as sapxep";

				sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
				sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
				sql+=" left join "+a.user+".a_dmtk dmtkno on ctct.tknoa=dmtkno.id";
				sql+=" left join "+a.user+".a_dmtk dmtkco on ctct.tkcoa=dmtkco.id";
				sql+=" left join "+a.user+".a_dmch dmchno on ctct.mtmno=dmchno.id";
				sql+=" left join "+a.user+".a_dmch dmchco on ctct.mtmco=dmchco.id";				
				sql+=" where ctct.tonghop<>9";				
				if (s_IDmuc!="") sql+=" and (ctct.mtmno in("+s_IDmuc+") or ctct.mtmco in("+s_IDmuc+"))";
				if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
				else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
				//sql+=" and to_date(to_char(ct.ngayg,'mm/yyyy'),'mm/yyyy') between to_date('"+s_Tungay+"','mm/yyyy') and to_date('"+s_Denngay+"','mm/yyyy')";

				if (s_IDchuong!="") sql+=" and ct.chuong='"+s_IDchuong+"'";
				if (s_IDlk!="") sql+=" and lk.id='"+s_IDlk+"'";
				if (s_IDhoatdong!="") sql+=" and ct.hoatdong='"+s_IDhoatdong+"'";
				if (s_IDnhombc!="") sql+=" and ct.idbc='"+s_IDnhombc+"'";
				sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
				sql+=" order by to_char(ct.ngayg,'yyyymm'),to_char(ct.ngayg,'yyyymmdd')";

				ds_Socai_muc.Merge(a.get_data(sql).Copy());
				if ((ds_Socai_muc==null || ds_Socai_muc.Tables[0].Rows.Count<=0)) flag_dsnull=true;
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Socai_muc;
		} 
		public System.Data.DataSet f_Soctcackhoanthu(LibKetoan.AccessData a,string s_IDchuong,string s_IDdoituong,string s_muc,string s_IDlk,string s_sotk,string s_IDkm, string s_IDkm2,string s_IDkm3, string s_IDkm4,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_yy,string s_yyyy,bool b_Checkngay,int i_dkgroup)
		{
			string s_DKgroup="";
			if(i_dkgroup!=-1) s_DKgroup=", "+i_dkgroup+" as nhom";
			else s_DKgroup=", -1 as nhom ";
			
			flag_dsnull=false;
			string s_Ngaydau="01/"+s_mm+"/"+s_yyyy;

			sql=" select to_char(ct.ngaygs, 'dd/mm/yy') as ngaygs, to_char(ct.ngayg, 'dd/mm/yy') as ngayct,";
			sql+=" hd.ten as hoatdong,ct.thanggs as thang, to_char(ct.ngaygs,'yyyy') as nam,";
			sql+=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
			sql+=" ch.ten as chuong,dt.tendt as doituong,bc.ten as nhombc,";
			sql+=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
			sql+=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2,";
			sql+=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
			sql+=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
			sql+=" ct.soctg as soctg, ctct.noidung as noidung, ctct.stien as stien, tkno.stk as stkno, tkco.stk as stkco ";
			sql+=" "+s_DKgroup+"";
			sql+=" from "+a.user+".a_ct"+s_yy+" ct inner join "+a.user+".a_ctct"+s_yy+" ctct on ct.id= ctct.idct";
			sql+=" left join "+a.user+".a_dmtk tkno on ctct.tknoa= tkno.id";
			sql+=" left join "+a.user+".a_dmtk tkco on ctct.tkcoa= tkco.id";
			sql+=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
			sql+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
			sql+=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";
			sql+=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";
			sql+=" left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";
			sql+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";
			sql+=" where ctct.tonghop=1";				
			sql+=" and (tkno.stk like '"+s_sotk+"%' or tkco.stk like '"+s_sotk+"%')";
			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Ngaydau+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";

			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";			
			
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and ctct.lk="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;
			if(s_IDdoituong!="")sql+=" and dt.id="+s_IDdoituong;
			if(s_muc!="")sql+=" and (chno.id in("+s_muc+") or chco.id in("+s_muc+"))";
			sql+=" order by ct.thanggs,ct.soctg";

			try
			{
				ds_Data=a.get_data(sql).Copy();
				if (ds_Data==null || ds_Data.Tables[0].Rows.Count<=0) flag_dsnull=true;
			}
			catch
			{
				flag_dsnull=true;
			}			
			if(flag_dsnull!=true)
			{				
				//them 1 cot fslk
				ds_Data.Tables[0].Columns.Add("Chifi01",typeof(System.Decimal));
				ds_Data.Tables[0].Columns.Add("Khac02",typeof(System.Decimal));
				ds_Data.Tables[0].Columns.Add("LaiCCP03",typeof(System.Decimal));
				ds_Data.Tables[0].Columns.Add("Tongthu01",typeof(System.Decimal));

				long l_Chifi=0,l_LaiCPP=0,l_Khac=0,l_Tongthu=0,l_tien=0;
				string stkno="", stkco="";
				int countRow=ds_Data.Tables[0].Rows.Count;
				for(int j=0;j<=countRow-1;j++)
				{
					//khoi tao lai gia tri cac bien
					l_Chifi=0;l_LaiCPP=0;l_Khac=0;l_Tongthu=0;
					stkno=ds_Data.Tables[0].Rows[j]["stkno"].ToString();
					stkco=ds_Data.Tables[0].Rows[j]["stkco"].ToString();
					l_tien=Convert.ToInt32(ds_Data.Tables[0].Rows[j]["stien"].ToString());
					if(stkco.Trim().Substring(0,3)=="631" && stkno.Trim().Substring(0,1)=="5")
					{
						l_Chifi+=l_tien;
					}
					if((stkco.Trim().Substring(0,3)=="421" && stkno.Trim().Substring(0,1)=="5")||(stkno.Trim().Substring(0,3)=="421" && stkco.Trim().Substring(0,1)=="5"))
					{
						l_LaiCPP+=l_tien;						
					}	
					if(stkco.Trim().Substring(0,3)!="421" && stkco.Trim().Substring(0,3)!="631" && stkno.Trim().Substring(0,2)=="51")		
						l_Khac+=l_tien;
					if(stkno.Trim().Substring(0,3)!="421" && stkco.Trim().Substring(0,1)=="5")
						l_Tongthu+=l_tien;
					ds_Data.Tables[0].Rows[j]["Chifi01"]=l_Chifi;
					ds_Data.Tables[0].Rows[j]["Khac02"]=l_Khac;
					ds_Data.Tables[0].Rows[j]["LaiCCP03"]=l_LaiCPP;
					ds_Data.Tables[0].Rows[j]["Tongthu01"]=l_Tongthu;
					ds_Data.AcceptChanges();
				}
			}
			try
			{
				if ((ds_Data==null || ds_Data.Tables[0].Rows.Count<=0)) flag_dsnull=true;
				ds_Data.WriteXml("..\\xml\\KT_Soctcackhoanthu.xml",XmlWriteMode.WriteSchema);
			}
			catch{};
			if (flag_dsnull) return null;
			else return ds_Data;

		} 
		private System.Data.DataSet f_Tinhsodu_luyke(LibKetoan.AccessData a,string s_IDchuong,string s_IDtk,string s_IDlk,string s_IDkm,string s_IDhoatdong,string s_IDnhombc,string s_mm,string s_yy,string s_yyyy,string s_IDdoituong,string s_muc,string s_IDkm2,string s_IDkm3,string s_IDkm4)
		{
			flag_dsnull=false;
			//Tao cau truc table cho DataSet.
			ds_Data=f_Socai_ct_muc_datablank(a).Clone();
			//Tinh so du dau ky cua thang va tinh luy ke tu dau nam.
			try
			{ 
				System.Data.DataRow dr_Sodu = ds_Data.Tables[0].NewRow();
				dr_Sodu["thangg"]=s_mm.PadLeft(2,'0')+"/"+s_yyyy.ToString();
				dr_Sodu["nhommuc"]="000";
				dr_Sodu["ngaygs"]="01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy;//DateTime.ParseExact(,"dd/MM/yyyy",null);
				dr_Sodu["soctg"]="";
				dr_Sodu["ngayg"]="01/"+s_mm.ToString().PadLeft(2,'0')+"/"+s_yyyy;//DateTime.ParseExact(,"dd/MM/yyyy",null);
				dr_Sodu["diengiai"]="Số dư đầu tháng";
				dr_Sodu["tkdoiung"]="";

				//Tham so cuoi: 1-lay so du no hoac phat sinh no, 2-lay so du co hoac phat sinh co.
				dr_Sodu["sotienno"]=0;
				dr_Sodu["sotienco"]=0;
						
				dr_Sodu["ghichu"]="---";
				dr_Sodu["loaictu"]=0;
				dr_Sodu["sapxep"]=int.Parse(s_yyyy+s_mm.PadLeft(2,'0')+"1");

				dr_Sodu["tondauno"]=a.get_Sodudauky(s_mm.PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);
				dr_Sodu["tondauco"]=a.get_Sodudauky(s_mm.PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);

				dr_Sodu["tondaumucno"]=0;
				dr_Sodu["tondaumucco"]=0;

				dr_Sodu["fsnoquy"]=a.get_FSluykequy(s_mm.PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);
				dr_Sodu["fscoquy"]=a.get_FSluykequy(s_mm.PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);

				dr_Sodu["fsnonam"]=a.get_FSluykenam(s_mm.PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,1,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);
				dr_Sodu["fsconam"]=a.get_FSluykenam(s_mm.PadLeft(2,'0'),s_yy,s_IDchuong,s_IDtk,s_IDlk,s_IDkm,s_IDhoatdong,s_IDnhombc,2,s_IDdoituong,s_muc,s_IDkm2,s_IDkm3,s_IDkm4);

				ds_Data.Tables[0].Rows.Add(dr_Sodu);
				ds_Data.AcceptChanges();
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Data;
		}
		#region Tien mat chi tiet
		/// <summary>		
		/// </summary>		
		public DataSet f_Soquytienmatchitiet(AccessData a,string s_IDchuong,string s_IDdoituong,string s_muc,string s_IDtk,string s_IDlk,string s_IDkm, string s_IDkm2,string s_IDkm3, string s_IDkm4,string s_IDhoatdong,string s_IDnhombc,string s_Tungay,string s_Denngay,string s_mm,string s_yy,string s_yyyy,bool b_Checkngay,int i_dkgroup)
		{
			decimal d_fsnongay = 0, d_fscongay = 0;

			string s_DKgroup = "";
			if (i_dkgroup != -1) s_DKgroup = ", " + i_dkgroup + " as nhom";
			else s_DKgroup = ", -1 as nhom ";

			string s_DKSelectgroup="",s_DKfrom="";

			switch(i_dkgroup)
			{
				case 0:
					s_DKSelectgroup=" ch.ten as chuong,ch.id as idchuong,";
					s_DKSelectgroup+=" '' as iddoituong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as doituong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmchuong ch on ct.chuong=ch.id";					
					break;
				case 1:
					s_DKSelectgroup=" dt.tendt as doituong,dt.id as iddoituong,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";					
					break;
				case 2:
					s_DKSelectgroup=" hd.ten as hoatdong,hd.id as idhoatdong,";
					s_DKSelectgroup+=" '' as idchuong,'' as iddoituong, '' as idnhombc,'' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong,'' as doituong, '' as nhombc,'' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_hoatdong hd on ct.hoatdong=hd.id";break;
				case 3:
					s_DKSelectgroup=" case when(chno.ttk <>' ') then chno.ttk else chco.ttk end as muc,";
					s_DKSelectgroup+=" case when(chno.ttk <>' ') then chno.id else chco.id end as idmuc,";
					s_DKSelectgroup+=" '' as idchuong,'' as iddoituong, '' as idnhombc,'' as idhoatdong, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong,'' as doituong, '' as nhombc,'' as hoatdong, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmch chco on ctct.mtmco=chco.id";
					s_DKfrom+=" left join "+a.user+".a_dmch chno on ctct.mtmno=chno.id";
					break;
				case 4:
					s_DKSelectgroup=" bc.ten as nhombc,bc.id as idnhombc,";
					s_DKSelectgroup+=" '' as idchuong, '' as iddoituong,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as doituong,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,";
					s_DKfrom="left join "+a.user+".a_dmnhombc bc on ct.idbc=bc.id";
					break;
				case 5:
					s_DKSelectgroup=" case when (kmno.ten <>' ') then kmno.ten else kmco.ten end as km,";
					s_DKSelectgroup+=" case when (kmno.ten <>' ') then kmno.id else kmco.id end as idkm,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as iddoituong, '' as idkm2, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as doituong, '' as km2, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno on ctct.kmno=kmno.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco on ctct.kmco=kmco.id";
					break;
				case 6:
					s_DKSelectgroup=" case when (kmno2.ten <>' ') then kmno2.ten else kmco2.ten end as km2,";
					s_DKSelectgroup+=" case when (kmno2.ten <>' ') then kmno2.id else kmco2.id end as idkm2,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as iddoituong, '' as idkm3, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as doituong, '' as km3, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno2 on ctct.kmno2=kmno2.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco2 on ctct.kmco2=kmco2.id";
					break;
				case 7:
					s_DKSelectgroup=" case when (kmno3.ten<>' ') then kmno3.ten else kmco3.ten end as km3,";
					s_DKSelectgroup+=" case when (kmno3.ten<>' ') then kmno3.id else kmco3.id end as idkm3,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as iddoituong, '' as idkm4,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as doituong, '' as km4,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno3 on ctct.kmno3=kmno3.id";
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco3 on ctct.kmco3=kmco3.id";
					break;
				case 8:
					s_DKSelectgroup=" case when (kmno4.ten<>' ') then kmno4.ten else kmco4.ten end as km4,";
					s_DKSelectgroup+=" case when (kmno4.ten<>' ') then kmno4.id else kmco4.id end as idkm4,";
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as iddoituong,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as doituong,";
					s_DKfrom=" left join "+a.user+".a_dmkhoanmuc kmno4 on ctct.kmno4=kmno4.id";	
					s_DKfrom+=" left join "+a.user+".a_dmkhoanmuc kmco4 on ctct.kmco4=kmco4.id";
					break;
				default: 					
					s_DKSelectgroup+=" '' as idchuong, '' as idnhombc,'' as idhoatdong, '' as idmuc, '' as idkm, '' as idkm2, '' as idkm3, '' as idkm4,'' as iddoituong,";
					s_DKSelectgroup+=" '' as chuong, '' as nhombc,'' as hoatdong, '' as muc, '' as km, '' as km2, '' as km3, '' as km4,'' as doituong,";
					break;
			}

			flag_dsnull = false;
			sql=  "select to_char(ct.ngaygs,'dd/mm/yyyy') as ngaygs,to_char(ct.ngayg,'dd/mm/yyyy') as ngayg,0 as sodu_no,0 as sodu_co,0 as fsluyke_no, 0 as fsluyke_co,";			
			sql+=" "+s_DKSelectgroup+"";
			sql+=" (case when ct.tknoa in("+s_IDtk+") then ct.soctg";
			sql+=" else '' end) as soctgthu,(case when ct.tkcoa in("+s_IDtk+") then ct.soctg else '' end)";
			sql+=" as soctgchi,nvl(dt.tendt,ctk.tendt) as hoten,ctct.noidung as diengiai,case when (";
			sql+=" ctct.tknoa in("+s_IDtk+")) then ctct.stien else 0 end as sotienthu,case when (ctct.tkcoa in("+s_IDtk+")";
			sql+=" )then ctct.stien else 0 end as sotienchi,0 as stton,0 as fsnothtruoc,0 as fscothtruoc,'' as ghichu,";
			sql+=" (case when (ct.tknoa in("+s_IDtk+")) then 1 else 2 end) as loaictu,";
			if (b_Checkngay) sql+=" (case when (to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy')<to_date('"+s_Tungay+"','dd/mm/yyyy')) then 99 else 2 end)as sapxep";
			else sql+=" 2 as sapxep";
			sql+=" "+s_DKgroup+" ";
	
			sql+=" from "+a.user+".a_ct"+s_yy+" ct "; 		
			sql+=" "+s_DKfrom+"";
			sql+=" left join "+a.user+".a_ctk"+s_yy+" ctk on ct.id=ctk.id";
			sql+=" left join "+a.user+".a_ctct"+s_yy+" ctct on ct.id=ctct.idct";
			sql+=" left join "+a.user+".a_dmlk lk on ctct.lk=lk.id";
			sql+=" left join "+a.user+".a_dmdt dt on ct.madt=dt.id";	

			sql+=" where ctct.tonghop=1 and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in ("+s_IDtk+"))";
			if (s_IDkm!="") sql+=" and (ctct.kmno like '"+s_IDkm+"' or ctct.kmco like '"+s_IDkm+"')";		
			if (s_IDkm2!="") sql+=" and (ctct.kmno2 like '"+s_IDkm2+"' or ctct.kmco2 like '"+s_IDkm2+"'";			
			if (s_IDkm3!="") sql+=" and (ctct.kmno3 like '"+s_IDkm3+"' or ctct.kmco3 like '"+s_IDkm3+"'";			
			if (s_IDkm4!="") sql+=" and (ctct.kmno4 like '"+s_IDkm4+"' or ctct.kmco4 like '"+s_IDkm4+"'";			

			if (b_Checkngay) sql+=" and to_date(to_char(ct.ngayg,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('"+s_Tungay+"','dd/mm/yyyy') and to_date('"+s_Denngay+"','dd/mm/yyyy')";
			else sql+=" and ct.thanggs between '"+s_Tungay+"' and '"+s_Denngay+"'";
				//sql+=" and to_date(to_char(ct.ngayg,'mm/yyyy'),'mm/yyyy') between to_date('"+s_Tungay+"','mm/yyyy') and to_date('"+s_Denngay+"','mm/yyyy')";
			if (s_IDchuong!="") sql+=" and ct.chuong="+s_IDchuong;
			if (s_IDlk!="") sql+=" and lk.id="+s_IDlk;
			if (s_IDhoatdong!="") sql+=" and ct.hoatdong="+s_IDhoatdong;
			if (s_IDnhombc!="") sql+=" and ct.idbc="+s_IDnhombc;
			if(s_IDdoituong!="")sql+=" and dt.id="+s_IDdoituong;
			if(s_muc!="")sql+=" and (ctct.mtmno in("+s_muc+") or ctct.mtmco in("+s_muc+"))";			
			sql+=" order by to_char(ct.ngaygs,'yyyymmdd'),loaictu,soctgchi,soctgthu";
			try
			{
				ds_Data = a.get_data(sql).Copy();
//				if (ds_Data == null || ds_Data.Tables[0].Rows.Count <= 0) flag_dsnull = true;
			}
			catch
			{
				flag_dsnull = true;
			}
			//Tinh luy ke tu dau thang den ngay hien tai.
			//ds_Data.WriteXml("..\\xml\\KT_Soquytienmatchitiet.xml",XmlWriteMode.WriteSchema);
			if (b_Checkngay) f_Tinh_chungtudauthang(ref ds_Data, "sotienthu", "sotienchi", "sapxep", "99", "ngayg", ref d_fsnongay, ref d_fscongay);

			try
			{
				decimal d_Dauky = 0, d_FSnamno = 0, d_FSnamco = 0, sumAll = 0;
				//Them dong load so du dau ky.

				System.Data.DataRow dr_Sodu = ds_Data.Tables[0].NewRow();
				dr_Sodu["ngaygs"] = "01/" + s_mm.ToString().PadLeft(2, '0') + "/" + s_yyyy;//DateTime.ParseExact(, "dd/MM/yyyy", null);
				dr_Sodu["ngayg"] = "01/" + s_mm.ToString().PadLeft(2, '0') + "/" + s_yyyy;//DateTime.ParseExact(, "dd/MM/yyyy", null);
				dr_Sodu["soctgthu"] = "";
				dr_Sodu["soctgchi"] = "";
				dr_Sodu["hoten"] = "";
				dr_Sodu["diengiai"] = "Số dư đầu kỳ";
				dr_Sodu["sotienthu"] = 0;
				dr_Sodu["sotienchi"] = 0;

				d_Dauky = a.get_Sodudauky(s_mm.ToString().PadLeft(2, '0'), s_yy, s_IDchuong, s_IDtk, s_IDlk, s_IDkm, s_IDhoatdong, s_IDnhombc, 3) + (d_fsnongay - d_fscongay);
				d_FSnamno = a.get_FSluykenam(s_mm.ToString().PadLeft(2, '0'), s_yy, s_IDchuong, s_IDtk, s_IDlk, s_IDkm, s_IDhoatdong, s_IDnhombc, 1) + d_fsnongay;
				d_FSnamco = a.get_FSluykenam(s_mm.ToString().PadLeft(2, '0'), s_yy, s_IDchuong, s_IDtk, s_IDlk, s_IDkm, s_IDhoatdong, s_IDnhombc, 2) + d_fscongay;
				sumAll = d_Dauky + d_FSnamno + d_FSnamco;

				dr_Sodu["stton"] = d_Dauky;
				dr_Sodu["fsnothtruoc"] = d_FSnamno;
				dr_Sodu["fscothtruoc"] = d_FSnamco;
				dr_Sodu["ghichu"] = "";
				dr_Sodu["loaictu"] = 0;
				dr_Sodu["sapxep"] = 1;

				ds_Data.Tables[0].Rows.Add(dr_Sodu);
				ds_Data.AcceptChanges();
				if (flag_dsnull && sumAll == 0) flag_dsnull = true;
				else flag_dsnull = false;
			}
			catch { };
			if (flag_dsnull) return null;
			else return ds_Data;
			//End tinh so du dau ky			
		} 		
		#endregion
		#region Dong tat ca cac ket noi va dataset khi ket thuc form
		public void Dispose()
		{
			try
			{
				ds_Data.Dispose();
				GC.Collect();
			}
			catch{};
		}
		#endregion
		
	}
}
